nx/e2e/js/src/js-executor-swc.test.ts
Jack Hsu 27edf71cef
feat(misc): make directory a required option for generators (#28093)
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

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

Fixes #

---------

Co-authored-by: Colum Ferry <cferry09@gmail.com>
Co-authored-by: Nicholas Cunningham <ndcunningham@gmail.com>
2024-10-01 09:29:44 -04:00

111 lines
2.7 KiB
TypeScript

import { execSync } from 'child_process';
import {
checkFilesExist,
cleanupProject,
newProject,
readJson,
runCLI,
tmpProjPath,
uniq,
updateFile,
updateJson,
} from '../../utils';
describe('js:swc executor', () => {
let scope: string;
beforeAll(() => {
scope = newProject();
});
afterAll(() => {
cleanupProject();
});
it('should create libs with js executors (--bundler=swc)', async () => {
const lib = uniq('lib');
runCLI(`generate @nx/js:lib libs/${lib} --bundler=swc --no-interactive`);
const libPackageJson = readJson(`libs/${lib}/package.json`);
expect(libPackageJson.scripts).toBeUndefined();
expect(runCLI(`build ${lib}`)).toContain(
'Successfully compiled: 2 files with swc'
);
checkFilesExist(
`dist/libs/${lib}/package.json`,
`dist/libs/${lib}/src/index.js`,
`dist/libs/${lib}/src/lib/${lib}.js`,
`dist/libs/${lib}/src/index.d.ts`,
`dist/libs/${lib}/src/lib/${lib}.d.ts`
);
const tsconfig = readJson(`tsconfig.base.json`);
expect(tsconfig.compilerOptions.paths).toEqual({
[`@${scope}/${lib}`]: [`libs/${lib}/src/index.ts`],
});
}, 240_000);
it('should handle swcrc path mappings', async () => {
const lib = uniq('lib');
runCLI(`generate @nx/js:lib libs/${lib} --bundler=swc --no-interactive`);
// add a dummy x.ts file for path mappings
updateFile(
`libs/${lib}/src/x.ts`,
`
export function x() {
console.log('x');
}
`
);
// update .swcrc to use path mappings
updateJson(`libs/${lib}/.swcrc`, (json) => {
json.jsc.baseUrl = '.';
json.jsc.paths = {
'~/*': ['./src/*'],
};
return json;
});
// update lib.ts to use x
updateFile(`libs/${lib}/src/lib/${lib}.ts`, () => {
return `
// @ts-ignore
import { x } from '~/x';
export function myLib() {
console.log(x());
}
myLib();
`;
});
// now run build without type checking (since we're using path mappings not in tsconfig)
runCLI(`build ${lib} --skipTypeCheck`);
// invoke the lib with node
const result = execSync(`node dist/libs/${lib}/src/lib/${lib}.js`, {
cwd: tmpProjPath(),
}).toString();
expect(result).toContain('x');
}, 240_000);
it('should support --strip-leading-paths option', () => {
const lib = uniq('lib');
runCLI(`generate @nx/js:lib libs/${lib} --bundler=swc --no-interactive`);
runCLI(`build ${lib} --stripLeadingPaths`);
checkFilesExist(
`dist/libs/${lib}/package.json`,
`dist/libs/${lib}/index.js`,
`dist/libs/${lib}/lib/${lib}.js`,
`dist/libs/${lib}/index.d.ts`,
`dist/libs/${lib}/lib/${lib}.d.ts`
);
});
});