Skip to content

Commit

Permalink
cleanup(misc): move things around and add extra test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
leosvelperez committed Dec 3, 2024
1 parent 86d3c51 commit d52145e
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 19 deletions.
54 changes: 54 additions & 0 deletions packages/angular/src/generators/utils/add-jest.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import type { Tree } from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { UnitTestRunner } from '../../utils/test-runners';
import { addJest } from './add-jest';
import { generateTestApplication } from './testing';

describe('addJest', () => {
let tree: Tree;

beforeEach(async () => {
tree = createTreeWithEmptyWorkspace();
await generateTestApplication(tree, {
name: 'app1',
directory: 'app1',
unitTestRunner: UnitTestRunner.None,
skipFormat: true,
});
});

it('generate the test setup file', async () => {
await addJest(tree, {
name: 'app1',
projectRoot: 'app1',
skipPackageJson: false,
strict: false,
});

expect(tree.read('app1/src/test-setup.ts', 'utf-8')).toMatchInlineSnapshot(`
"import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone';
setupZoneTestEnv();
"
`);
});

it('generate the test setup file with strict', async () => {
await addJest(tree, {
name: 'app1',
projectRoot: 'app1',
skipPackageJson: false,
strict: true,
});

expect(tree.read('app1/src/test-setup.ts', 'utf-8')).toMatchInlineSnapshot(`
"import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone';
setupZoneTestEnv({
errorOnUnknownElements: true,
errorOnUnknownProperties: true
});
"
`);
});
});
30 changes: 11 additions & 19 deletions packages/angular/src/generators/utils/add-jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,17 @@ export async function addJest(
'src',
'test-setup.ts'
);
if (tree.exists(setupFile)) {
if (options.strict && tree.exists(setupFile)) {
const contents = tree.read(setupFile, 'utf-8');
if (options.strict) {
tree.write(
setupFile,
`${contents}
setupZoneTestEnv({
errorOnUnknownElements: true,
errorOnUnknownProperties: true
});
`
);
} else {
tree.write(
setupFile,
`${contents}
setupZoneTestEnv();
`
);
}
tree.write(
setupFile,
contents.replace(
'setupZoneTestEnv();',
`setupZoneTestEnv({
errorOnUnknownElements: true,
errorOnUnknownProperties: true
});`
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ describe('jestProject', () => {
expect(tree.read('libs/lib1/src/test-setup.ts', 'utf-8'))
.toMatchInlineSnapshot(`
"import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone';
setupZoneTestEnv();
"
`);
expect(tree.exists('libs/lib1/jest.config.ts')).toBeTruthy();
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
import { setupZoneTestEnv } from 'jest-preset-angular/setup-env/zone';

setupZoneTestEnv();

0 comments on commit d52145e

Please sign in to comment.