Skip to content

Commit

Permalink
test: add additional npm specs
Browse files Browse the repository at this point in the history
  • Loading branch information
erickzhao committed Dec 4, 2020
1 parent 5e3a588 commit 4c6d449
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/renderer/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,13 @@ export function packageRun(
return exec(dir, `${packageManager} run ${command}`);
}

function decommentWithWorker(input: string): Promise<string> {
export function decommentWithWorker(input: string): Promise<string> {
return new Promise((resolve, reject) => {
const worker = new Worker('../utils/decomment.ts');
worker.postMessage(input);
worker.onmessage = function (event: MessageEvent<string>) {
resolve(event.data);
};
worker.postMessage(input);
worker.onerror = function (e) {
reject(e);
};
Expand Down
20 changes: 20 additions & 0 deletions tests/mocks/worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import * as decomment from 'decomment';

export default class MockWorker {
public onmessage: (msg: any) => any;
public onmessageerror: () => any;
public onerror: () => any;
public addEventListener: () => any;
public removeEventListener: () => any;
public dispatchEvent: () => any;
public terminate: () => any;
constructor() {
this.onmessage = () => {
/*no-op*/
};
}

public postMessage(input: string) {
this.onmessage({ data: decomment(input) });
}
}
59 changes: 50 additions & 9 deletions tests/renderer/npm-spec.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
import { mocked } from 'ts-jest/utils';
import * as decomment from 'decomment';

import {
findModules,
findModulesInEditors,
getIsPackageManagerInstalled,
installModules,
packageRun,
} from '../../src/renderer/npm';
import { exec } from '../../src/utils/exec';
import { overridePlatform, resetPlatform } from '../utils';

import MockDecommentWorker from '../mocks/worker';
jest.mock('decomment');
jest.mock('../../src/utils/exec');
jest.mock('../../src/utils/import', () => ({
fancyImport: async (_p: string) => {
return { default: require('builtin-modules') };
},
}));

describe('npm', () => {
const mockMain = `
const say = require('say');
window.Worker = MockDecommentWorker;

describe('npm', () => {
const mockBuiltins = `
function hello() {
const electron = require('electron');
const originalFs = require('original-fs');
Expand All @@ -26,6 +31,20 @@ describe('npm', () => {
}
`;

const mockPackages = `
const cow = require('cow');
const say = require('say');
`;

const mockComments = `
// const cow = require('cow');
/* const say = require('say'); */
/**
* const hello = require('hello');
* const world = require('world');
*/
`;

describe('getIsPackageManagerInstalled()', () => {
describe('npm()', () => {
beforeEach(() => {
Expand Down Expand Up @@ -150,17 +169,39 @@ describe('npm', () => {
});
});

describe('findModules()', () => {
it('returns required modules in a JS file', async () => {
mocked(decomment).mockReturnValue(mockPackages);
const modules = await findModules(mockPackages);
expect(modules).toEqual(['cow', 'say']);
});

it('ignores node and electron builtins', async () => {
mocked(decomment).mockReturnValue(mockBuiltins);
const modules = await findModules(mockBuiltins);
expect(modules).toHaveLength(0);
});

it('ignores commented modules', async () => {
mocked(decomment).mockReturnValue('');
const modules = await findModules(mockComments);
expect(modules).toHaveLength(0);
});
});

describe('findModulesInEditors()', () => {
it('finds modules, ignoring node and electron builtins', async () => {
it('installs modules across all JavaScript files only once', async () => {
mocked(decomment).mockReturnValue(mockPackages);
const result = await findModulesInEditors({
html: '',
main: mockMain,
renderer: '',
preload: '',
main: mockPackages,
renderer: mockPackages,
preload: mockPackages,
css: '',
});

expect(result).toEqual(['say']);
expect(result).toHaveLength(2);
expect(result).toEqual(['cow', 'say']);
});
});

Expand Down

0 comments on commit 4c6d449

Please sign in to comment.