/
react-package.test.ts
154 lines (132 loc) · 4.86 KB
/
react-package.test.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
import {
checkFilesDoNotExist,
checkFilesExist,
ensureProject,
forEachCli,
readJson,
runCLI,
uniq,
updateFile,
} from '@nrwl/e2e/utils';
forEachCli('nx', (cli) => {
describe('Build React libraries and apps', () => {
/**
* Graph:
*
* childLib
* /
* app => parentLib =>
* \
* childLib2
*
*/
let app: string;
let parentLib: string;
let childLib: string;
let childLib2: string;
beforeEach(() => {
app = uniq('app');
parentLib = uniq('parentlib');
childLib = uniq('childlib');
childLib2 = uniq('childlib2');
ensureProject();
runCLI(`generate @nrwl/react:app ${app}`);
runCLI(
`generate @nrwl/react:library ${parentLib} --buildable --no-interactive`
);
runCLI(
`generate @nrwl/react:library ${childLib} --buildable --no-interactive`
);
runCLI(
`generate @nrwl/react:library ${childLib2} --buildable --no-interactive`
);
// create dependencies by importing
const createDep = (parent, children: string[]) => {
updateFile(
`libs/${parent}/src/lib/${parent}.tsx`,
`
${children.map((entry) => `import '@proj/${entry}';`).join('\n')}
`
);
};
createDep(parentLib, [childLib, childLib2]);
updateFile(
`apps/${app}/src/main.tsx`,
`
import "@proj/${parentLib}";
`
);
// we are setting paths to {} to make sure built libs are read from dist
updateFile('tsconfig.json', (c) => {
const json = JSON.parse(c);
json.compilerOptions.paths = {};
return JSON.stringify(json, null, 2);
});
// Add assets to child lib
updateFile(cli === 'angular' ? 'angular.json' : 'workspace.json', (c) => {
const json = JSON.parse(c);
json.projects[childLib].architect.build.options.assets = [
`libs/${childLib}/src/assets`,
];
return JSON.stringify(json, null, 2);
});
updateFile(`libs/${childLib}/src/assets/hello.txt`, 'Hello World!');
});
it('should throw an error if the dependent library has not been built before building the parent lib', () => {
expect.assertions(2);
try {
runCLI(`build ${parentLib}`);
} catch (e) {
expect(e.stderr.toString()).toContain(
`Some of the project ${parentLib}'s dependencies have not been built yet. Please build these libraries before:`
);
expect(e.stderr.toString()).toContain(`${childLib}`);
}
});
it('should build the library when it does not have any deps', () => {
const output = runCLI(`build ${childLib}`);
expect(output).toContain(`${childLib}.esm.js`);
expect(output).toContain(`Bundle complete`);
checkFilesExist(`dist/libs/${childLib}/assets/hello.txt`);
});
it('should copy the README to dist', () => {
const output = runCLI(`build ${childLib2}`);
expect(output).toContain(`Bundle complete`);
checkFilesExist(`dist/libs/${childLib2}/README.md`);
});
it('should properly add references to any dependency into the parent package.json', () => {
const childLibOutput = runCLI(`build ${childLib}`);
const childLib2Output = runCLI(`build ${childLib2}`);
const parentLibOutput = runCLI(`build ${parentLib}`);
expect(childLibOutput).toContain(`${childLib}.esm.js`);
expect(childLibOutput).toContain(`${childLib}.umd.js`);
expect(childLibOutput).toContain(`Bundle complete`);
expect(childLib2Output).toContain(`${childLib2}.esm.js`);
expect(childLib2Output).toContain(`${childLib2}.umd.js`);
expect(childLib2Output).toContain(`Bundle complete`);
expect(parentLibOutput).toContain(`${parentLib}.esm.js`);
expect(parentLibOutput).toContain(`${parentLib}.umd.js`);
expect(parentLibOutput).toContain(`Bundle complete`);
const jsonFile = readJson(`dist/libs/${parentLib}/package.json`);
expect(jsonFile.dependencies).toEqual(
expect.objectContaining({
[`@proj/${childLib}`]: '0.0.1',
[`@proj/${childLib2}`]: '0.0.1',
react: expect.anything(),
})
);
});
it('should build an app composed out of buildable libs', () => {
const buildWithDeps = runCLI(`build ${app} --with-deps`);
expect(buildWithDeps).toContain(`Running target "build" succeeded`);
checkFilesDoNotExist(`apps/${app}/tsconfig/tsconfig.nx-tmp`);
// we remove all path mappings from the root tsconfig, so when trying to build
// libs from source, the builder will throw
const failedBuild = runCLI(
`build ${app} --with-deps --buildLibsFromSource`,
{ silenceError: true }
);
expect(failedBuild).toContain(`Can't resolve`);
}, 1000000);
});
});