/
next.test.ts
203 lines (175 loc) · 5.35 KB
/
next.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import { capitalize } from '@nrwl/workspace/src/utils/strings';
import { fork } from 'child_process';
import * as http from 'http';
import {
checkFilesExist,
ensureProject,
forEachCli,
readFile,
runCLI,
runCLIAsync,
supportUi,
tmpProjPath,
uniq,
updateFile
} from './utils';
import treeKill = require('tree-kill');
forEachCli('nx', () => {
describe('Next.js Applications', () => {
it('should generate a Next.js app that consumes a react lib', async () => {
ensureProject();
const appName = uniq('app');
const libName = uniq('lib');
runCLI(
`generate @nrwl/next:app ${appName} --no-interactive --linter=eslint`
);
runCLI(`generate @nrwl/react:lib ${libName} --no-interactive`);
const mainPath = `apps/${appName}/pages/index.tsx`;
updateFile(mainPath, `import '@proj/${libName}';\n` + readFile(mainPath));
updateFile(
`apps/${appName}/next.config.js`,
`
module.exports = {
generateBuildId: function () {
return 'fixed';
}
};
`
);
await checkApp(appName, { checkLint: true });
// check that the configuration was consumed
expect(readFile(`dist/apps/${appName}/BUILD_ID`)).toEqual('fixed');
}, 120000);
it('should generate a Next.js app dynamically loading a lib', async () => {
ensureProject();
const appName = uniq('app');
const libName = uniq('lib');
runCLI(
`generate @nrwl/next:app ${appName} --no-interactive --linter=eslint`
);
runCLI(`generate @nrwl/react:lib ${libName} --no-interactive`);
const mainPath = `apps/${appName}/pages/index.tsx`;
updateFile(
mainPath,
`
import dynamic from 'next/dynamic';
const DynamicComponent = dynamic(
() => import('@proj/${libName}').then(d => d.${capitalize(libName)})
);
` + readFile(mainPath)
);
await checkApp(appName, { checkLint: false });
}, 120000);
it('should generate a Next.js app that compiles when using a workspace and react lib written in TypeScript', async () => {
ensureProject();
const appName = uniq('app');
const tsLibName = uniq('tslib');
const tsxLibName = uniq('tsxlib');
runCLI(
`generate @nrwl/next:app ${appName} --no-interactive --linter=eslint`
);
runCLI(`generate @nrwl/react:lib ${tsxLibName} --no-interactive`);
runCLI(`generate @nrwl/workspace:lib ${tsLibName} --no-interactive`);
updateFile(
`libs/${tsLibName}/src/lib/${tsLibName}.ts`,
`
export function testFn(): string {
return 'Hello Nx';
};
`
);
updateFile(
`libs/${tsxLibName}/src/lib/${tsxLibName}.tsx`,
`
import React from 'react';
interface TestComponentProps {
text: string;
}
export const TestComponent = ({ text }: TestComponentProps) => {
return <span>{text}</span>;
};
export default TestComponent;
`
);
const mainPath = `apps/${appName}/pages/index.tsx`;
const content = readFile(mainPath);
updateFile(
mainPath,
`
import { testFn } from '@proj/${tsLibName}';
import { TestComponent } from '@proj/${tsxLibName}';\n\n
` +
content.replace(
`<main>`,
`<main>
<>
{testFn()}
<TestComponent text="Hello Next.JS" />
</>
`
)
);
await checkApp(appName, { checkLint: true });
}, 120000);
});
});
async function checkApp(appName: string, opts: { checkLint: boolean }) {
if (opts.checkLint) {
const lintResults = runCLI(`lint ${appName}`);
expect(lintResults).toContain('All files pass linting.');
}
const testResults = await runCLIAsync(`test ${appName}`);
expect(testResults.stderr).toContain('Test Suites: 1 passed, 1 total');
// This adds about 80s to the e2e run
// expectAppToRun(appName);
const buildResult = runCLI(`build ${appName}`);
expect(buildResult).toContain(`Compiled successfully`);
checkFilesExist(`dist/apps/${appName}/build-manifest.json`);
const exportResult = runCLI(`export ${appName}`);
expect(exportResult).toContain('Exporting (3/3)');
checkFilesExist(`dist/apps/${appName}/exported/index.html`);
}
async function expectAppToRun(appName: string) {
const server = fork(
`./node_modules/@nrwl/cli/bin/nx.js`,
[`serve`, appName],
{
cwd: tmpProjPath(),
silent: true
}
);
expect(server).toBeTruthy();
await new Promise(resolve => {
setTimeout(() => {
getPage().then(page => {
expect(page).toContain(`Here are some things you can do with Nx`);
treeKill(server.pid, 'SIGTERM', err => {
expect(err).toBeFalsy();
resolve();
});
});
}, 5000);
});
if (supportUi()) {
const e2eResults = runCLI(`e2e ${appName}-e2e`);
expect(e2eResults).toContain('All specs passed!');
}
}
function getPage(): Promise<string> {
return new Promise(resolve => {
http.get('http://localhost:4200/', res => {
let data = '';
res.on('data', chunk => {
data += chunk;
});
res.once('end', () => {
resolve(data);
});
});
});
}
forEachCli('angular', () => {
describe('next', () => {
it('is not supported', () => {});
});
});