Skip to content

Commit

Permalink
feat(js): move workspace tests
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav committed Apr 6, 2023
1 parent 50812a3 commit 7ca3c12
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 85 deletions.
83 changes: 79 additions & 4 deletions e2e/js/src/js.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {
checkFilesDoNotExist,
checkFilesExist,
cleanupProject,
createFile,
expectJestTestsToPass,
newProject,
readJson,
runCLI,
runCLIAsync,
uniq,
updateFile,
updateJson,
Expand Down Expand Up @@ -87,7 +89,80 @@ export function ${lib}Wildcard() {
);
}, 240_000);

it('should run default jest tests', async () => {
await expectJestTestsToPass('@nrwl/js:lib');
}, 240_000);
it('should create a library that can be linted and tested', async () => {
const libName = uniq('mylib');
const dirName = uniq('dir');

runCLI(`generate @nrwl/js:lib ${libName} --directory ${dirName}`);

checkFilesExist(
`libs/${dirName}/${libName}/src/index.ts`,
`libs/${dirName}/${libName}/README.md`
);

// Lint
const result = runCLI(`lint ${dirName}-${libName}`);

expect(result).toContain(`Linting "${dirName}-${libName}"...`);
expect(result).toContain('All files pass linting.');

// Test
const testResult = await runCLIAsync(`test ${dirName}-${libName}`);
expect(testResult.combinedOutput).toContain(
'Test Suites: 1 passed, 1 total'
);
}, 500_000);

it('should be able to use and be used by other libs', () => {
const consumerLib = uniq('consumer');
const producerLib = uniq('producer');

runCLI(`generate @nrwl/js:lib ${consumerLib} --bundler=none`);
runCLI(`generate @nrwl/js:lib ${producerLib} --bundler=none`);

updateFile(
`libs/${producerLib}/src/lib/${producerLib}.ts`,
'export const a = 0;'
);

updateFile(
`libs/${consumerLib}/src/lib/${consumerLib}.ts`,
`
import { a } from '@${scope}/${producerLib}';
export function ${consumerLib}() {
return a + 1;
}`
);
updateFile(
`libs/${consumerLib}/src/lib/${consumerLib}.spec.ts`,
`
import { ${consumerLib} } from './${consumerLib}';
describe('', () => {
it('should return 1', () => {
expect(${consumerLib}()).toEqual(1);
});
});`
);

runCLI(`test ${consumerLib}`);
});

it('should not be able to be built when it has no bundler', () => {
const nonBuildable = uniq('buildable');

runCLI(`generate @nrwl/js:lib ${nonBuildable} --bundler=none`);

const result = runCLI(`build ${nonBuildable}`);

expect(result).toContain(
`Compiling TypeScript files for project "${nonBuildable}"...`
);
expect(result).toContain(
`Done compiling TypeScript files for project "${nonBuildable}".`
);

checkFilesDoNotExist(`dist/libs/${nonBuildable}/README.md`);
});
});
81 changes: 0 additions & 81 deletions e2e/nx-misc/src/workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
runCLI,
uniq,
updateFile,
expectJestTestsToPass,
readFile,
exists,
updateProjectConfig,
Expand All @@ -27,86 +26,6 @@ describe('Workspace Tests', () => {

afterAll(() => cleanupProject());

describe('@nrwl/js:library', () => {
it('should create a library that can be tested and linted', async () => {
const libName = uniq('mylib');
const dirName = uniq('dir');

runCLI(`generate @nrwl/js:lib ${libName} --directory ${dirName}`);

checkFilesExist(
`libs/${dirName}/${libName}/src/index.ts`,
`libs/${dirName}/${libName}/README.md`
);

// Lint
const result = runCLI(`lint ${dirName}-${libName}`);

expect(result).toContain(`Linting "${dirName}-${libName}"...`);
expect(result).toContain('All files pass linting.');

// Test
await expectJestTestsToPass('@nrwl/js:lib');
}, 100000);

it('should be able to use and be used by other libs', () => {
const consumerLib = uniq('consumer');
const producerLib = uniq('producer');

runCLI(`generate @nrwl/js:lib ${consumerLib}`);
runCLI(`generate @nrwl/js:lib ${producerLib}`);

updateFile(
`libs/${producerLib}/src/lib/${producerLib}.ts`,
'export const a = 0;'
);

updateFile(
`libs/${consumerLib}/src/lib/${consumerLib}.ts`,
`
import { a } from '@${proj}/${producerLib}';
export function ${consumerLib}() {
return a + 1;
}`
);
updateFile(
`libs/${consumerLib}/src/lib/${consumerLib}.spec.ts`,
`
import { ${consumerLib} } from './${consumerLib}';
describe('', () => {
it('should return 1', () => {
expect(${consumerLib}()).toEqual(1);
});
});`
);

runCLI(`test ${consumerLib}`);
});

it('should be able to be built when it is buildable', () => {
const buildableLib = uniq('buildable');

runCLI(`generate @nrwl/js:lib ${buildableLib} --buildable`);

const result = runCLI(`build ${buildableLib}`);

expect(result).toContain(
`Compiling TypeScript files for project "${buildableLib}"...`
);
expect(result).toContain(
`Done compiling TypeScript files for project "${buildableLib}".`
);

checkFilesExist(`dist/libs/${buildableLib}/README.md`);

const json = readJson(`dist/libs/${buildableLib}/package.json`);
expect(json.main).toEqual('./src/index.js');
expect(json.types).toEqual('./src/index.d.ts');
});
});

describe('@nrwl/workspace:npm-package', () => {
it('should create a minimal npm package', () => {
const npmPackage = uniq('npm-package');
Expand Down

0 comments on commit 7ca3c12

Please sign in to comment.