Skip to content

Commit

Permalink
feat: initial de-coupling of @angular-devkit from tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bennymeg committed Nov 23, 2021
1 parent 26634e5 commit d210d4a
Show file tree
Hide file tree
Showing 11 changed files with 2,409 additions and 996 deletions.
1,591 changes: 1,518 additions & 73 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"private": true,
"devDependencies": {
"@nrwl/angular": "^12.10.0",
"@nrwl/cli": "^12.10.0",
"@nrwl/devkit": "^12.10.0",
"@nrwl/eslint-plugin-nx": "^12.10.0",
Expand Down
174 changes: 81 additions & 93 deletions packages/nx-electron/src/executors/build/executor.spec.ts
Original file line number Diff line number Diff line change
@@ -1,122 +1,110 @@
import { normalize, JsonObject, workspaces } from '@angular-devkit/core';
import { join, resolve } from 'path';
jest.mock('tsconfig-paths-webpack-plugin');
import TsConfigPathsPlugin from 'tsconfig-paths-webpack-plugin';
import { BuildElectronBuilderOptions } from './executor';
import { ExecutorContext } from '@nrwl/devkit';
import * as projectGraph from '@nrwl/workspace/src/core/project-graph';
import type { ProjectGraph } from '@nrwl/workspace/src/core/project-graph';
import { of } from 'rxjs';
import * as buildWebpack from '@angular-devkit/build-webpack';
import { Architect } from '@angular-devkit/architect';
import { getTestArchitect } from '../../utils/testing';
import executor, { BuildElectronBuilderOptions } from './executor';

jest.mock('../../utils/run-webpack', () => ({
runWebpack: jest.fn(),
}));

import { runWebpack } from '../../utils/run-webpack';

describe('ElectronBuildBuilder', () => {
let testOptions: BuildElectronBuilderOptions & JsonObject;
let architect: Architect;
let runWebpack: jest.Mock;
let context: ExecutorContext;
let options: BuildElectronBuilderOptions;

beforeEach(async () => {
[architect] = await getTestArchitect();
jest
.spyOn(projectGraph, 'readCachedProjectGraph')
.mockReturnValue({} as ProjectGraph);

testOptions = {
(<any>runWebpack).mockReturnValue(of({ hasErrors: () => false }));

context = {
root: '/root',
cwd: '/root',
projectName: 'my-app',
targetName: 'build',
workspace: {
version: 2,
projects: {
'my-app': <any>{
root: 'apps/electron-app',
sourceRoot: 'apps/electron-app',
},
},
},
isVerbose: false,
};

options = {
main: 'apps/electron-app/src/main.ts',
tsConfig: 'apps/electron-app/tsconfig.app.json',
tsConfig: 'apps/electron-app/tsconfig.ts',
outputPath: 'dist/apps/electron-app',
externalDependencies: 'all',
implicitDependencies: [],
fileReplacements: [
{
replace: 'apps/environment/environment.ts',
with: 'apps/environment/environment.prod.ts'
},
{
replace: 'module1.ts',
with: 'module2.ts'
}
],
buildLibsFromSource: true,
fileReplacements: [],
assets: [],
statsJson: false,
root: join(normalize(resolve(__dirname).replace(/build.*$/, '')), 'apps', 'electron-app', 'src')
};
runWebpack = jest.fn().mockImplementation((config, context, options) => {
options.logging({
toJson: () => ({
stats: 'stats'
})
});
return of({ success: true });
});
(buildWebpack as any).runWebpack = runWebpack;
spyOn(workspaces, 'readWorkspace').and.returnValue({
workspace: {
projects: {
get: () => ({
sourceRoot: join(normalize(resolve(__dirname).replace(/build.*$/, '')), 'apps', 'electron-app', 'src')
})
}
}
});
(<any>TsConfigPathsPlugin).mockImplementation(
function MockPathsPlugin() {}
);
});

describe('run', () => {
it('should call runWebpack', async () => {
const run = await architect.scheduleBuilder(
'nx-electron:build',
testOptions
);
await run.output.toPromise();
afterEach(() => jest.clearAllMocks());

await run.stop();
it('should call webpack', async () => {
await executor(options, context).next();

expect(runWebpack).toHaveBeenCalled();
});

it('should emit the outfile along with success', async () => {
const run = await architect.scheduleBuilder(
'nx-electron:build',
testOptions
expect(runWebpack).toHaveBeenCalledWith(
expect.objectContaining({
output: expect.objectContaining({
filename: 'main.js',
libraryTarget: 'commonjs',
path: '/root/dist/apps/electron-app',
}),
})
);
const output = await run.output.toPromise();
});

await run.stop();
it('should use outputFileName if passed in', async () => {
await executor(
{ ...options, outputFileName: 'index.js' },
context
).next();

expect(output.success).toEqual(true);
expect(output.outfile).toEqual(join(normalize(resolve(__dirname).replace(/build.*$/, '')), 'dist', 'apps', 'electron-app', 'main.js'));
expect(runWebpack).toHaveBeenCalledWith(
expect.objectContaining({
output: expect.objectContaining({
filename: 'index.js',
libraryTarget: 'commonjs',
path: '/root/dist/apps/wibble',
}),
})
);
});

describe('webpackConfig option', () => {
it('should require the specified function and use the return value', async () => {
const mockFunction = jest.fn(config => ({
config: 'config'
}));
describe('webpackConfig', () => {
it('should handle custom path', async () => {
jest.mock(
join(normalize(resolve(__dirname).replace(/build.*$/, '')), 'apps', 'electron-app', 'webpack.config.js'),
() => mockFunction,
{
virtual: true
}
);
testOptions.webpackConfig = 'apps/electron-app/webpack.config.js';
const run = await architect.scheduleBuilder(
'nx-electron:build',
testOptions
'/root/config.js',
() => (options) => ({ ...options, prop: 'my-val' }),
{ virtual: true }
);
await run.output.toPromise();
await executor(
{ ...options, webpackConfig: 'config.js' },
context
).next();

await run.stop();

expect(mockFunction).toHaveBeenCalled();
expect(runWebpack).toHaveBeenCalledWith(
{
config: 'config'
},
jasmine.anything(),
jasmine.anything()
expect.objectContaining({
output: expect.objectContaining({
filename: 'main.js',
libraryTarget: 'commonjs',
path: '/root/dist/apps/wibble',
}),
prop: 'my-val',
})
);
// expect(runWebpack.calls.first().args[0]).toEqual({
// config: 'config'
// });
});
});
});
Expand Down
Loading

0 comments on commit d210d4a

Please sign in to comment.