/
karma-project.spec.ts
156 lines (129 loc) · 5.02 KB
/
karma-project.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import type { Tree } from '@nrwl/devkit';
import * as devkit from '@nrwl/devkit';
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import { karmaProjectGenerator } from './karma-project';
import libraryGenerator from '../library/library';
import { Linter } from '@nrwl/linter';
import { UnitTestRunner } from '../../utils/test-runners';
import applicationGenerator from '../application/application';
describe('karmaProject', () => {
let tree: Tree;
beforeEach(async () => {
tree = createTreeWithEmptyWorkspace();
await libraryGenerator(tree, {
name: 'lib1',
buildable: false,
linter: Linter.EsLint,
publishable: false,
simpleModuleName: false,
skipFormat: false,
unitTestRunner: UnitTestRunner.None,
});
await applicationGenerator(tree, {
name: 'app1',
unitTestRunner: UnitTestRunner.None,
});
});
it('should throw when there is already a test target', async () => {
devkit.updateJson(tree, 'workspace.json', (json) => {
json.projects['lib1'].architect.test = {};
return json;
});
await expect(
karmaProjectGenerator(tree, { project: 'lib1' })
).rejects.toThrow('"lib1" already has a test target.');
});
it('should generate files', async () => {
expect(tree.exists('karma.conf.js')).toBeFalsy();
await karmaProjectGenerator(tree, { project: 'lib1' });
expect(tree.exists('/libs/lib1/karma.conf.js')).toBeTruthy();
expect(tree.exists('/libs/lib1/tsconfig.spec.json')).toBeTruthy();
expect(tree.exists('/libs/lib1/src/test.ts')).toBeTruthy();
expect(tree.exists('karma.conf.js')).toBeTruthy();
});
it('should create a karma.conf.js', async () => {
await karmaProjectGenerator(tree, { project: 'lib1' });
const karmaConf = tree.read('libs/lib1/karma.conf.js').toString();
expect(karmaConf).toMatchSnapshot();
});
it('should update the project tsconfig.json to reference the tsconfig.spec.json', async () => {
await karmaProjectGenerator(tree, { project: 'lib1' });
const tsConfig = devkit.readJson(tree, 'libs/lib1/tsconfig.json');
expect(tsConfig.references).toContainEqual({
path: './tsconfig.spec.json',
});
});
it('should format files', async () => {
jest.spyOn(devkit, 'formatFiles');
await karmaProjectGenerator(tree, { project: 'lib1' });
expect(devkit.formatFiles).toHaveBeenCalled();
});
describe('library', () => {
it('should update the workspace config correctly', async () => {
await karmaProjectGenerator(tree, { project: 'lib1' });
const workspaceJson = devkit.readJson(tree, 'workspace.json');
expect(workspaceJson.projects.lib1.architect.test).toEqual({
builder: '@angular-devkit/build-angular:karma',
options: {
main: 'libs/lib1/src/test.ts',
tsConfig: 'libs/lib1/tsconfig.spec.json',
karmaConfig: 'libs/lib1/karma.conf.js',
},
});
});
it('should create a tsconfig.spec.json', async () => {
await karmaProjectGenerator(tree, { project: 'lib1' });
const tsConfig = devkit.readJson(tree, 'libs/lib1/tsconfig.spec.json');
expect(tsConfig).toEqual({
extends: './tsconfig.json',
compilerOptions: {
outDir: '../../dist/out-tsc',
types: ['jasmine', 'node'],
},
files: ['src/test.ts'],
include: ['**/*.spec.ts', '**/*.test.ts', '**/*.d.ts'],
});
});
it('should create a test.ts', async () => {
await karmaProjectGenerator(tree, { project: 'lib1' });
const testTs = tree.read('libs/lib1/src/test.ts').toString();
expect(testTs).toMatchSnapshot();
});
});
describe('applications', () => {
it('should update the workspace config correctly', async () => {
await karmaProjectGenerator(tree, { project: 'app1' });
const workspaceJson = devkit.readJson(tree, 'workspace.json');
expect(workspaceJson.projects.app1.architect.test).toEqual({
builder: '@angular-devkit/build-angular:karma',
options: {
main: 'apps/app1/src/test.ts',
polyfills: 'apps/app1/src/polyfills.ts',
tsConfig: 'apps/app1/tsconfig.spec.json',
karmaConfig: 'apps/app1/karma.conf.js',
styles: [],
scripts: [],
assets: [],
},
});
});
it('should create a tsconfig.spec.json', async () => {
await karmaProjectGenerator(tree, { project: 'app1' });
const tsConfig = devkit.readJson(tree, 'apps/app1/tsconfig.spec.json');
expect(tsConfig).toEqual({
extends: './tsconfig.json',
compilerOptions: {
outDir: '../../dist/out-tsc',
types: ['jasmine', 'node'],
},
files: ['src/test.ts', 'src/polyfills.ts'],
include: ['**/*.spec.ts', '**/*.test.ts', '**/*.d.ts'],
});
});
it('should create a test.ts', async () => {
await karmaProjectGenerator(tree, { project: 'app1' });
const testTs = tree.read('apps/app1/src/test.ts').toString();
expect(testTs).toMatchSnapshot();
});
});
});