nx/e2e/web/src/file-server.test.ts
Jack Hsu 42d9e8bcb3
feat(web): add support for TS solution setup for @nx/web (#29583)
This PR adds the new TS setup support to `@nx/web:app` generator.
Previously it errored out since it was not handled.

## Current Behavior
Cannot generate webapp in new setup/

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->
Can generate webapp in new setup

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
2025-01-10 13:52:36 -05:00

109 lines
2.6 KiB
TypeScript

import {
cleanupProject,
killPorts,
newProject,
promisifiedTreeKill,
runCLI,
runCommandUntil,
uniq,
updateFile,
updateJson,
} from '@nx/e2e/utils';
import { join } from 'path';
describe('file-server', () => {
beforeAll(() => {
newProject({ name: uniq('fileserver') });
});
afterAll(() => cleanupProject());
it('should serve folder of files', async () => {
const appName = uniq('app');
const port = 4301;
runCLI(
`generate @nx/web:app apps/${appName} --no-interactive --bundler=webpack`
);
updateJson(join('apps', appName, 'project.json'), (config) => {
config.targets['serve'] = {
executor: '@nx/web:file-server',
options: {
buildTarget: 'build',
},
};
return config;
});
const p = await runCommandUntil(
`serve ${appName} --port=${port}`,
(output) => {
return output.indexOf(`localhost:${port}`) > -1;
}
);
try {
await promisifiedTreeKill(p.pid, 'SIGKILL');
await killPorts(port);
} catch {
// ignore
}
}, 300_000);
it('should read from directory from outputs if outputPath is not specified', async () => {
const appName = uniq('app');
const port = 4301;
runCLI(
`generate @nx/web:app apps/${appName} --no-interactive --bundler=webpack`
);
// Used to copy index.html rather than the normal webpack build.
updateFile(
`apps/${appName}/copy-index.js`,
`
const fs = require('node:fs');
const path = require('node:path');
fs.mkdirSync(path.join(__dirname, '../../dist/foobar'), { recursive: true });
fs.copyFileSync(
path.join(__dirname, './src/index.html'),
path.join(__dirname, '../../dist/foobar/index.html')
);
`
);
updateJson(join('apps', appName, 'project.json'), (config) => {
// Point to same path as output.path in webpack config.
config.targets['build'] = {
command: `node copy-index.js`,
outputs: [`{workspaceRoot}/dist/foobar`],
options: {
cwd: '{projectRoot}',
},
};
config.targets['serve'] = {
executor: '@nx/web:file-server',
options: {
buildTarget: 'build',
},
};
return config;
});
const p = await runCommandUntil(
`serve ${appName} --port=${port}`,
(output) => {
return (
output.indexOf(`localhost:${port}`) > -1 &&
output.indexOf(`dist/foobar`) > -1
);
}
);
try {
await promisifiedTreeKill(p.pid, 'SIGKILL');
await killPorts(port);
} catch {
// ignore
}
}, 300_000);
});