Skip to content

Commit

Permalink
Feature/jest in core (#899)
Browse files Browse the repository at this point in the history
* WIP

* Replace ava with jest in core

* Update package-lock

* Exclude tests from transpilation

* Expect transformPluginFile to throw

* Mock fetch in audius tests

* Mock fetch in audius tests
nukeop authored Apr 5, 2021
1 parent d78e21e commit f268871
Showing 22 changed files with 4,785 additions and 3,068 deletions.
9 changes: 9 additions & 0 deletions packages/core/__mocks__/electron-store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Electron {
prototype = {}
get() {
return '';
}
set() {}
}

module.exports = Electron;
6 changes: 6 additions & 0 deletions packages/core/__mocks__/electron-timber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class MockLogger {
static log() {}
static error() {}
}

module.exports = MockLogger;
17 changes: 17 additions & 0 deletions packages/core/__mocks__/electron.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
app: {
getPath: () => { }
},
remote: {
transformSource: () => { },
app: {
getPath: () => { }
},
dialog: {
showOpenDialog: jest.fn(async () => Promise.resolve({
canceled: false,
filePaths: ['test file.txt']
}))
}
}
};
File renamed without changes.
7 changes: 7 additions & 0 deletions packages/core/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

export default async () => ({
transform: {
'^.+\\.(ts|tsx)$': 'ts-jest',
'^.+\\.(js|jsx)$': 'ts-jest'
}
});
7,307 changes: 4,503 additions & 2,804 deletions packages/core/package-lock.json

Large diffs are not rendered by default.

20 changes: 5 additions & 15 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -14,50 +14,40 @@
"main": "src/index.ts",
"scripts": {
"lint": "eslint \"{test,src}/**/**.{js,ts}\" --fix",
"test": "eslint \"{test,src}/**/**.{js,ts}\" && ava -v"
"test": "eslint \"{test,src}/**/**.{js,ts}\" && jest"
},
"dependencies": {
"@babel/core": "^7.7.7",
"@babel/preset-env": "^7.7.7",
"@babel/preset-react": "^7.7.4",
"babel-jest": "^26.6.3",
"bandcamp-scraper": "git://github.com/nukeop/bandcamp-scraper.git",
"cheerio": "^1.0.0-rc.5",
"electron-store": "^5.1.0",
"electron-timber": "^0.5.1",
"get-artist-title": "^1.2.0",
"get-lyrics-hd": "^0.0.1",
"isomorphic-fetch": "^2.2.1",
"jest": "^26.6.3",
"md5": "^2.2.1",
"nodebrainz": "^2.1.1",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"redux": "^4.0.5",
"ts-jest": "^26.5.4",
"youtube-playlist": "^1.0.2",
"ytsr": "^3.2.3"
},
"devDependencies": {
"@types/isomorphic-fetch": "0.0.35",
"@types/jest": "^26.0.22",
"@types/lodash": "^4.14.150",
"@types/md5": "^2.1.33",
"@types/react": "^16.9.17",
"ava": "^2.4.0",
"eslint": "^7.18.0",
"mock-require": "^3.0.3",
"ts-node": "^8.10.2",
"typescript": "^3.9.7",
"ytdl-core": "^4.4.5"
},
"ava": {
"files": [
"test/**",
"!test/plugins"
],
"compileEnhancements": false,
"extensions": [
"ts"
],
"require": [
"ts-node/register"
]
}
}
30 changes: 30 additions & 0 deletions packages/core/src/plugins/plugins.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import path from 'path';

import { transformSource, transformPluginFile } from '..';

describe('Plugin tests', () => {
it('transform a simple plugin', async () => {
const result: any = await transformPluginFile(path.resolve(__dirname, 'testData/simple.js'));
expect(() => eval(result.code)).not.toThrow();
});

it('throw an error when a nonexistent path is provided', async () => {
expect(transformPluginFile('test')).rejects.toThrow();
});

it('transform some code', async () => {
const result: any = await transformSource('const f = (a,b) => a+b; const test = f(2,3); export default test;');
expect(eval(result.code)).toEqual(5);
});

it('throw an error on invalid code', async () => {
expect(transformSource('()asd')).rejects.toThrow();
});

it('transform jsx', async () => {
const result: any = await transformSource(
'import React from \'react\'; export const component = () => <div />;'
);
expect(() => eval(result.code)).not.toThrow();
});
});
File renamed without changes.
43 changes: 43 additions & 0 deletions packages/core/src/rest/audius.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import _ from 'lodash';
import { rest } from '..';

const mockFetch = (data) => {
global.fetch = jest.fn(() =>
Promise.resolve({
json: jest.fn(() => ({
data
}))
})
) as any;
};


describe('Audius tests', () => {
beforeEach(() => {
_.invoke(fetch, 'resetMocks');
});

it('audius host is selected', async () => {
mockFetch(['1', '2', '3']);
const endpoint = await rest.Audius._findHost();
expect(endpoint.length > 0).toBe(true);
});

it('search artists', async () => {
mockFetch('test');
const endpoint = await rest.Audius._findHost();
mockFetch([{ test: 'test data' }]);
const response = await rest.Audius.artistSearch(endpoint, 'roto');
const json = await response.json();
expect(json.data).toEqual([{ test: 'test data'}]);
});

it('search tracks', async () => {
mockFetch('test');
const endpoint = await rest.Audius._findHost();
mockFetch([{ test: 'test data' }]);
const response = await rest.Audius.trackSearch(endpoint, 'roto');
const json = await response.json();
expect(json.data).toEqual([{ test: 'test data'}]);
});
});
27 changes: 27 additions & 0 deletions packages/core/src/rest/bandcamp.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'isomorphic-fetch';
import { Bandcamp } from '.';

describe('Bandcamp tests', () => {
it('search', async () => {
const result = await Bandcamp.search('swans');
expect(result.length > 0).toBe(true);
});

it('get album info', async () => {
const result = await Bandcamp.getAlbumInfo('https://swans.bandcamp.com/album/the-seer');
expect(result.tracks.length === 11).toBe(true);
expect(result.artist).toBe('SWANS');
expect(result.title).toBe('The Seer');
expect(result.imageUrl).toBe('https://f4.bcbits.com/img/a3233794906_2.jpg');
expect(result.url).toBe('https://swans.bandcamp.com/album/the-seer');
});

it('get track stream', async () => {
const { duration, stream } = await Bandcamp.getTrackData('https://swans.bandcamp.com/track/apostate');
expect(stream.length).toBeGreaterThan(0);
expect(stream.includes('https')).toBe(true);
expect(stream.includes('stream')).toBe(true);
expect(stream.includes('mp3-128')).toBe(true);
expect(duration).toBeGreaterThan(0);
});
});
54 changes: 54 additions & 0 deletions packages/core/src/rest/lastfm.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { rest } from '..';

const setupLastFmApi = (key: string, secret: string): rest.LastFmApi => {
return new rest.LastFmApi(key, secret);
};

describe('Last.fm tests', () => {
it('add api key to url', () => {
const api = setupLastFmApi('test', 'test');
const url = 'http://example.com?test=test1&test2=test3';
const withKey = api.addApiKey(url);

expect(withKey).toBe('http://example.com?test=test1&test2=test3&api_key=test');
});

it('sign url', () => {
const api = setupLastFmApi('test', 'test');
const url = 'http://example.com?test=test1&test2=test3';
const signed = api.sign(url);

expect(signed).toBe('4dd7efc68ff9d7c293ac0f71eb133ace');
});

it('prepare url', () => {
const api = setupLastFmApi('test', 'test');
const url = 'http://example.com?test=test1&test2=test3';
const prepared = api.prepareUrl(url);

expect(prepared).toBe('http://example.com?test=test1&test2=test3&api_key=test&api_sig=cd28b8fd248073c89aad9b77dd069567');
});

it('get top tracks', async () => {
const api = setupLastFmApi('2b75dcb291e2b0c9a2c994aca522ac14',
'2ee49e35f08b837d43b2824198171fc8');

const response = await api.getTopTracks();
const data = await response.json();

expect(typeof data.tracks).toBe('object');
expect(data.tracks.track instanceof Array).toBe(true);
});

it('search tracks', async () => {
const api = setupLastFmApi('2b75dcb291e2b0c9a2c994aca522ac14',
'2ee49e35f08b837d43b2824198171fc8');

const response = await api.searchTracks('billie jean');
const data = await response.json();

expect(typeof data.results).toBe('object');
expect(data.results.trackmatches.track instanceof Array).toBe(true);
expect(data.results.trackmatches.track.length).toBeGreaterThan(0);
});
});
45 changes: 45 additions & 0 deletions packages/core/src/rest/sponsorblock.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { rest } from '..';
import { OriginalSegment } from './SponsorBlock.types';

import { MockServer, VIDEO_ID } from '../../__mocks__/sponsor-block-server';

describe('Sponsorblock tests', () => {
it('has empty result', () => {
const segments = rest.SponsorBlock.formatResponse(MockServer.request(VIDEO_ID.HAS_NO_SEGMENT) as OriginalSegment[]);
expect(segments.length).toBe(0);
});

it('has result', () => {
const segments = rest.SponsorBlock.formatResponse(MockServer.request(VIDEO_ID.HAS_SEGMENT) as OriginalSegment[]);
expect(segments instanceof Array).toBe(true);
expect(segments.length).toBeGreaterThan(0);
});

it('result is ascendingly ordered by startTime', () => {
const segments = rest.SponsorBlock.formatResponse(MockServer.request(VIDEO_ID.HAS_SEGMENT_NOT_ORDER) as OriginalSegment[]);
expect(segments instanceof Array).toBe(true);
expect(segments.every(
(segment, i, arr) => !i || (segment.startTime > arr[i - 1].startTime))).toBe(true);
});

it('result has no segment contained in other segment ', () => {
const segments = rest.SponsorBlock.formatResponse(MockServer.request(VIDEO_ID.HAS_SEGMENT_CONTAIN_OTHER_SEGMENT) as OriginalSegment[]);
expect(segments instanceof Array).toBe(true);
expect(segments.every(
(segment, i, arr) => {
for (let j = 0; j < arr.length; j += 1) {
if (j === i) {
continue;
}
// check whether this segment is contained in another segment
// j------i--------i-----j
// start start end end
if (arr[j].startTime <= segment.startTime &&
arr[j].endTime >= segment.endTime) {
return false;
}
}
return true;
})).toBe(true);
});
});
35 changes: 0 additions & 35 deletions packages/core/test/audius.test.ts

This file was deleted.

39 changes: 0 additions & 39 deletions packages/core/test/bandcamp.test.ts

This file was deleted.

75 changes: 0 additions & 75 deletions packages/core/test/lastfm.test.ts

This file was deleted.

41 changes: 0 additions & 41 deletions packages/core/test/plugins.test.ts

This file was deleted.

53 changes: 0 additions & 53 deletions packages/core/test/sponsorblock.test.ts

This file was deleted.

3 changes: 2 additions & 1 deletion packages/core/tsconfig.json
Original file line number Diff line number Diff line change
@@ -16,7 +16,8 @@
},
"include": [
"src",
"typings"
"typings",
"test"
],
"exclude": [
"lib"
37 changes: 33 additions & 4 deletions packages/main/package-lock.json
4 changes: 3 additions & 1 deletion packages/main/package.json
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
"version": "0.6.11",
"description": "Nuclear main process",
"scripts": {
"start": "shx rm -rf build && webpack --env.NODE_ENV=development && electron build/main.js",
"start": "shx rm -rf build && webpack --env.NODE_ENV=development && electron build/main.js --inspect=9001",
"build": "webpack --colors --env.NODE_ENV=production",
"lint": "eslint src/**/**.ts --fix"
},
@@ -52,6 +52,7 @@
"typeorm": "^0.2.22",
"url": "^0.11.0",
"uuid": "^3.3.3",
"v8-compile-cache": "^2.3.0",
"ytdl-core": "^4.4.5"
},
"devDependencies": {
@@ -74,6 +75,7 @@
"eslint": "^7.18.0",
"node-gyp": "^7.0.0",
"node-loader": "^0.6.0",
"shx": "^0.3.3",
"ts-loader": "^6.2.2",
"ts-node": "^8.10.2",
"type-fest": "^0.8.1",
1 change: 1 addition & 0 deletions packages/main/tsconfig.json
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@
],
"exclude": [
"node_modules/@nuclear/*/test",
"node_modules/@nuclear/**/*.test*",
"node_modules/@nuclear/ui/stories",
"../ui/stories",
"../*/test"

0 comments on commit f268871

Please sign in to comment.