Skip to content

Commit

Permalink
add e2e test for CommonJS/ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
cometkim committed Jan 24, 2024
1 parent 4f3aa26 commit f65d64a
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions tests/specs/e2e.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import {execUtils} from '@yarnpkg/core';
import {xfs, PortablePath, npath} from '@yarnpkg/fslib';

describe(`E2E`, () => {
it(`works with CommonJS`, async () => {
await xfs.mktempPromise(async tempDir => {
const rndName = Math.floor(Math.random() * 100000000).toString(16).padStart(8, `0`);

const packed = await execUtils.execvp(`yarn`, [`pack`, `--out`, npath.fromPortablePath(`${tempDir}/${rndName}.tgz`)], {
cwd: npath.toPortablePath(__dirname),
});

expect(packed.code).toEqual(0);

await xfs.writeJsonPromise(`${tempDir}/package.json` as PortablePath, {name: `test-treeshake`});
await xfs.writeFilePromise(`${tempDir}/yarn.lock` as PortablePath, ``);

const added = await execUtils.execvp(`yarn`, [`add`, `./${rndName}.tgz`], {cwd: tempDir});
expect(added.code).toEqual(0);

await xfs.writeFilePromise(`${tempDir}/index.cjs` as PortablePath,
`const {Command, Option, runExit} = require('clipanion');
runExit(class MainCommand extends Command {
name = Option.String();
async execute() {
this.context.stdout.write(\`Hello \${this.name}!\\n\`);
}
})`,
);

const result = await execUtils.execvp(`node`, [`${tempDir}/index.cjs`, 'World'], {cwd: tempDir});
expect(result.code).toEqual(0);
expect(result.stdout).toEqual('Hello World!\n');
});
}, 20000);

it(`works with ESM`, async () => {
await xfs.mktempPromise(async tempDir => {
const rndName = Math.floor(Math.random() * 100000000).toString(16).padStart(8, `0`);

const packed = await execUtils.execvp(`yarn`, [`pack`, `--out`, npath.fromPortablePath(`${tempDir}/${rndName}.tgz`)], {
cwd: npath.toPortablePath(__dirname),
});

expect(packed.code).toEqual(0);

await xfs.writeJsonPromise(`${tempDir}/package.json` as PortablePath, {name: `test-treeshake`});
await xfs.writeFilePromise(`${tempDir}/yarn.lock` as PortablePath, ``);

const added = await execUtils.execvp(`yarn`, [`add`, `./${rndName}.tgz`], {cwd: tempDir});
expect(added.code).toEqual(0);

await xfs.writeFilePromise(`${tempDir}/index.mjs` as PortablePath,
`import {Command, Option, runExit} from 'clipanion';
runExit(class MainCommand extends Command {
name = Option.String();
async execute() {
this.context.stdout.write(\`Hello \${this.name}!\\n\`);
}
})`,
);

const result = await execUtils.execvp(`node`, [`${tempDir}/index.mjs`, 'World'], {cwd: tempDir});
expect(result.code).toEqual(0);
expect(result.stdout).toEqual('Hello World!\n');
});
}, 20000);
});

0 comments on commit f65d64a

Please sign in to comment.