Skip to content

Commit

Permalink
fix(nx): exclude appropriate test setup file in tsconfig.lib.json
Browse files Browse the repository at this point in the history
Resolves #1139
  • Loading branch information
catfireparty authored and vsavkin committed Sep 5, 2019
1 parent 0b72c63 commit eeb713f
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 8 deletions.
45 changes: 38 additions & 7 deletions packages/angular/src/schematics/library/library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,44 @@ describe('lib', () => {
expect(tsconfigJson.extends).toEqual('./tsconfig.json');
});

it('should extend the local tsconfig.json with tsconfig.lib.json', async () => {
const tree = await runSchematic('lib', { name: 'myLib' }, appTree);
const tsconfigJson = readJsonInTree(
tree,
'libs/my-lib/tsconfig.lib.json'
);
expect(tsconfigJson.extends).toEqual('./tsconfig.json');
describe('when creating the tsconfig.lib.json', () => {
it('should extend the local tsconfig.json', async () => {
const tree = await runSchematic('lib', { name: 'myLib' }, appTree);
const tsconfigJson = readJsonInTree(
tree,
'libs/my-lib/tsconfig.lib.json'
);
expect(tsconfigJson.extends).toEqual('./tsconfig.json');
});

it('should exclude the test setup file when unitTestRunner is jest', async () => {
const tree = await runSchematic(
'lib',
{ name: 'myLib', unitTestRunner: 'jest' },
appTree
);
const tsconfigJson = readJsonInTree(
tree,
'libs/my-lib/tsconfig.lib.json'
);
expect(tsconfigJson.exclude).toEqual([
'src/test-setup.ts',
'**/*.spec.ts'
]);
});

it('should leave the excludes alone when unitTestRunner is not jest', async () => {
const tree = await runSchematic(
'lib',
{ name: 'myLib', unitTestRunner: 'karma' },
appTree
);
const tsconfigJson = readJsonInTree(
tree,
'libs/my-lib/tsconfig.lib.json'
);
expect(tsconfigJson.exclude).toEqual(['src/test.ts', '**/*.spec.ts']);
});
});

it('should generate files', async () => {
Expand Down
6 changes: 5 additions & 1 deletion packages/angular/src/schematics/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,11 @@ function updateProject(options: NormalizedSchema): Rule {
compilerOptions: {
...json.compilerOptions,
outDir: `${offsetFromRoot(options.projectRoot)}dist/out-tsc`
}
},
exclude:
options.unitTestRunner === 'jest'
? ['src/test-setup.ts', '**/*.spec.ts']
: json.exclude || []
};
}),
updateJsonInTree(`${options.projectRoot}/tslint.json`, json => {
Expand Down
5 changes: 5 additions & 0 deletions packages/workspace/migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
"version": "8.4.0-beta.1",
"description": "Add 'nx' script to package.json",
"factory": "./src/migrations/update-8-4-0/add-nx-script"
},
"fix-tsconfig-lib-json": {
"version": "8.5.0-beta.1",
"description": "Fix the exclude paths in tsconfig.lib.json files for Jest projects",
"factory": "./src/migrations/update-8-5-0/fix-tsconfig-lib-json"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Tree, SchematicContext } from '@angular-devkit/schematics';
import { readWorkspace, readJsonInTree } from '../../utils/ast-utils';

export default function() {
return (host: Tree) => {
const config = readWorkspace(host);

const configsToUpdate = [];
Object.keys(config.projects).forEach(name => {
const project = config.projects[name];
if (
project.projectType === 'library' &&
project.architect &&
project.architect.test &&
project.architect.test.builder === '@nrwl/jest:test' &&
project.architect.test.options &&
project.architect.test.options.setupFile ===
project.sourceRoot + '/test-setup.ts'
) {
configsToUpdate.push(project.root + '/tsconfig.lib.json');
}
});

configsToUpdate.forEach(config => {
const tsconfig = readJsonInTree(host, config);
if (tsconfig.exclude && tsconfig.exclude[0] === 'src/test.ts') {
tsconfig.exclude[0] = 'src/test-setup.ts';
host.overwrite(config, JSON.stringify(tsconfig));
}
});
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { Tree } from '@angular-devkit/schematics';
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
import { join } from 'path';
import { readJsonInTree } from '@nrwl/workspace';
import { createEmptyWorkspace } from '@nrwl/workspace/testing';
import { createLibWithTests } from '../../utils/testing';

describe('Update 8.5.0', () => {
let tree: Tree;
let schematicRunner: SchematicTestRunner;

const jestBuilder = '@nrwl/jest:test';
const nonJestBuilder = 'something-else';

beforeEach(async () => {
tree = Tree.empty();
tree = createEmptyWorkspace(tree);
schematicRunner = new SchematicTestRunner(
'@nrwl/workspace',
join(__dirname, '../../../migrations.json')
);
});

describe('When fixing tsconfig.lib.json files', () => {
const schematicName = 'fix-tsconfig-lib-json';

it('should not modify non jest projects', async () => {
const lib = 'non-jest-lib';
const exclude = [`src/test.ts`, '**/*.spec.ts'];

tree = await createLibWithTests(tree, lib, nonJestBuilder, 'test.ts');
tree.create(
`/libs/${lib}/tsconfig.lib.json`,
JSON.stringify({ exclude })
);

const result = await schematicRunner
.runSchematicAsync(schematicName, {}, tree)
.toPromise();

const tsconfigJson = readJsonInTree(
result,
`libs/${lib}/tsconfig.lib.json`
);
expect(tsconfigJson.exclude).toEqual(exclude);
});

it('should modify untouched jest projects', async () => {
const lib = 'jest-lib';
const exclude = [`src/test.ts`, '**/*.spec.ts'];
const expected = ['src/test-setup.ts', '**/*.spec.ts'];

tree = await createLibWithTests(tree, lib, jestBuilder, 'test-setup.ts');
tree.create(
`/libs/${lib}/tsconfig.lib.json`,
JSON.stringify({ exclude })
);

const result = await schematicRunner
.runSchematicAsync(schematicName, {}, tree)
.toPromise();

const tsconfigJson = readJsonInTree(
result,
`libs/${lib}/tsconfig.lib.json`
);

expect(tsconfigJson.exclude).toEqual(expected);
});

it('should not touch modified jest projects', async () => {
const lib = 'modified-jest-lib';
const exclude = [`src/test-modified.ts`, '**/*.spec.ts'];

tree = await createLibWithTests(
tree,
lib,
jestBuilder,
'test-modified.ts.ts'
);
tree.create(
`/libs/${lib}/tsconfig.lib.json`,
JSON.stringify({ exclude })
);

const result = await schematicRunner
.runSchematicAsync(schematicName, {}, tree)
.toPromise();

const tsconfigJson = readJsonInTree(
result,
`libs/${lib}/tsconfig.lib.json`
);
expect(tsconfigJson.exclude).toEqual(exclude);
});
});
});
33 changes: 33 additions & 0 deletions packages/workspace/src/utils/testing.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { join } from 'path';
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
import { Rule, Tree } from '@angular-devkit/schematics';
import { names } from './name-utils';
import { updateWorkspace } from './workspace';

const testRunner = new SchematicTestRunner(
'@nrwl/workspace',
Expand All @@ -25,3 +27,34 @@ export function runMigration(migrationName: string, options: any, tree: Tree) {
.runSchematicAsync(migrationName, options, tree)
.toPromise();
}

export function createLibWithTests(
tree: Tree,
libName: string,
testBuilder: string,
testSetupFile: string
): Promise<Tree> {
const { fileName } = names(libName);

tree.create(`/libs/${fileName}/src/index.ts`, `\n`);

return callRule(
updateWorkspace(workspace => {
workspace.projects.add({
name: fileName,
root: `libs/${fileName}`,
projectType: 'library',
sourceRoot: `libs/${fileName}/src`,
architect: {
test: {
builder: testBuilder,
options: {
setupFile: `libs/${fileName}/src/${testSetupFile}`
}
}
}
});
}),
tree
);
}

0 comments on commit eeb713f

Please sign in to comment.