/
npm-package.spec.ts
114 lines (101 loc) · 3.25 KB
/
npm-package.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
import {
getProjects,
readJson,
readProjectConfiguration,
readWorkspaceConfiguration,
Tree,
updateWorkspaceConfiguration,
writeJson,
} from '@nrwl/devkit';
import { createTreeWithEmptyWorkspace } from '@nrwl/devkit/testing';
import { getWorkspacePath } from '@nrwl/devkit';
import { npmPackageGenerator } from './npm-package';
describe('@nrwl/workspace:npm-package', () => {
let tree: Tree;
beforeEach(() => {
tree = createTreeWithEmptyWorkspace();
const workspaceConfig = readWorkspaceConfiguration(tree);
workspaceConfig.workspaceLayout = {
appsDir: 'packages',
libsDir: 'packages',
};
updateWorkspaceConfiguration(tree, workspaceConfig);
});
it('should generate a minimal package', async () => {
await npmPackageGenerator(tree, {
name: 'my-package',
});
const project = readProjectConfiguration(tree, 'my-package');
expect(project).toMatchInlineSnapshot(`
Object {
"root": "packages/my-package",
}
`);
});
it('should only generate package.json and index.js', async () => {
await npmPackageGenerator(tree, {
name: 'my-package',
});
expect(tree.read('packages/my-package/package.json').toString())
.toMatchInlineSnapshot(`
"{
\\"name\\": \\"@proj/my-package\\",
\\"version\\": \\"0.0.0\\",
\\"scripts\\": {
\\"test\\": \\"node index.js\\"
}
}
"
`);
expect(tree.read('packages/my-package/index.js').toString())
.toMatchInlineSnapshot(`
"console.log('Hello World');
"
`);
});
describe('for existing projects', () => {
it('should not modify files, only add configuration', async () => {
const existingPackageJson = {
name: 'my-existing-package',
scripts: {
'existing-script': 'existing',
},
};
const existingIndex = `export * from './src'`;
writeJson(tree, 'packages/my-package/package.json', existingPackageJson);
tree.write('packages/my-package/index.ts', existingIndex);
await npmPackageGenerator(tree, {
name: 'my-package',
});
const { projects } = readJson(tree, 'workspace.json');
expect(projects['my-package']).toMatchInlineSnapshot(`
Object {
"root": "packages/my-package",
}
`);
expect(readJson(tree, 'packages/my-package/package.json')).toEqual(
existingPackageJson
);
expect(tree.read('packages/my-package/index.ts').toString()).toEqual(
existingIndex
);
});
});
describe('for workspaces without workspace.json', () => {
it('should not create workspace.json or project.json', async () => {
tree.delete(getWorkspacePath(tree));
await npmPackageGenerator(tree, {
name: 'my-package',
});
expect(tree.exists('workspace.json')).toBeFalsy();
expect(tree.exists('angular.json')).toBeFalsy();
expect(tree.exists('packages/my-package/project.json')).toBeFalsy();
expect(tree.exists('packages/my-package/package.json')).toBeTruthy();
expect(readProjectConfiguration(tree, 'my-package')).toEqual({
projectType: 'library',
root: 'packages/my-package',
sourceRoot: 'packages/my-package',
});
});
});
});