Skip to content

Commit 9a57592

Browse files
committed
test: add more APIPlugin interception
1 parent 2de7357 commit 9a57592

File tree

2 files changed

+64
-49
lines changed

2 files changed

+64
-49
lines changed
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
export const c = require.cache;
1+
export const a = require.cache;
2+
export const b = require.extensions;
3+
export const c = require.config;
4+
export const d = require.version;
5+
export const e = require.include;
6+
export const f = require.onError;
Lines changed: 58 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import path from 'node:path';
2-
import { expect, test } from '@rstest/core';
3-
import { buildAndGetResults } from 'test-helper';
1+
import path from "node:path";
2+
import { expect, test } from "@rstest/core";
3+
import { buildAndGetResults } from "test-helper";
44

5-
test('format default to esm', async () => {
6-
const fixturePath = path.resolve(__dirname, 'default');
7-
const { files, contents } = await buildAndGetResults({
8-
fixturePath,
9-
});
5+
test("format default to esm", async () => {
6+
const fixturePath = path.resolve(__dirname, "default");
7+
const { files, contents } = await buildAndGetResults({
8+
fixturePath,
9+
});
1010

11-
expect(files).toMatchInlineSnapshot(`
11+
expect(files).toMatchInlineSnapshot(`
1212
{
1313
"cjs0": [
1414
"<ROOT>/tests/integration/format/default/dist/bundle-cjs/index.cjs",
@@ -27,7 +27,7 @@ test('format default to esm', async () => {
2727
}
2828
`);
2929

30-
expect(contents.esm0).toMatchInlineSnapshot(`
30+
expect(contents.esm0).toMatchInlineSnapshot(`
3131
{
3232
"<ROOT>/tests/integration/format/default/dist/bundle-esm/index.js": "const foo = 'foo';
3333
const str = 'hello' + foo + ' world';
@@ -36,7 +36,7 @@ test('format default to esm', async () => {
3636
}
3737
`);
3838

39-
expect(contents.esm1).toMatchInlineSnapshot(`
39+
expect(contents.esm1).toMatchInlineSnapshot(`
4040
{
4141
"<ROOT>/tests/integration/format/default/dist/bundleless-esm/foo.js": "const foo = 'foo';
4242
export { foo };
@@ -49,47 +49,47 @@ test('format default to esm', async () => {
4949
`);
5050
});
5151

52-
test('import.meta.url should be preserved', async () => {
53-
const fixturePath = path.resolve(__dirname, 'import-meta-url');
54-
const { files, entries, entryFiles } = await buildAndGetResults({
55-
fixturePath,
56-
});
57-
expect(files).toMatchInlineSnapshot(`
52+
test("import.meta.url should be preserved", async () => {
53+
const fixturePath = path.resolve(__dirname, "import-meta-url");
54+
const { files, entries, entryFiles } = await buildAndGetResults({
55+
fixturePath,
56+
});
57+
expect(files).toMatchInlineSnapshot(`
5858
{
5959
"esm": [
6060
"<ROOT>/tests/integration/format/import-meta-url/dist/esm/index.js",
6161
],
6262
}
6363
`);
64-
expect(entries.esm).toMatchInlineSnapshot(`
64+
expect(entries.esm).toMatchInlineSnapshot(`
6565
"import node_url from "node:url";
6666
const packageDirectory = node_url.fileURLToPath(new URL('.', import.meta.url));
6767
const foo = 'foo';
6868
export { foo, packageDirectory };
6969
"
7070
`);
7171

72-
const result = await import(entryFiles.esm);
73-
expect(result).toMatchInlineSnapshot(`
72+
const result = await import(entryFiles.esm);
73+
expect(result).toMatchInlineSnapshot(`
7474
{
7575
"foo": "foo",
7676
"packageDirectory": "<ROOT>/tests/integration/format/import-meta-url/dist/esm/",
7777
}
7878
`);
7979
});
8080

81-
test('CJS exports should be statically analyzable (cjs-module-lexer for Node.js)', async () => {
82-
const fixturePath = path.resolve(__dirname, 'cjs-static-export');
83-
const { entryFiles } = await buildAndGetResults({
84-
fixturePath,
85-
});
81+
test("CJS exports should be statically analyzable (cjs-module-lexer for Node.js)", async () => {
82+
const fixturePath = path.resolve(__dirname, "cjs-static-export");
83+
const { entryFiles } = await buildAndGetResults({
84+
fixturePath,
85+
});
8686

87-
const { bar, foo } = await import(entryFiles.cjs);
88-
expect(bar).toBe('bar');
89-
expect(foo).toBe('foo');
87+
const { bar, foo } = await import(entryFiles.cjs);
88+
expect(bar).toBe("bar");
89+
expect(foo).toBe("foo");
9090

91-
const cjs = await import(entryFiles.cjs);
92-
expect(cjs).toMatchInlineSnapshot(`
91+
const cjs = await import(entryFiles.cjs);
92+
expect(cjs).toMatchInlineSnapshot(`
9393
{
9494
"bar": "bar",
9595
"default": {
@@ -101,28 +101,38 @@ test('CJS exports should be statically analyzable (cjs-module-lexer for Node.js)
101101
`);
102102
});
103103

104-
test('throw error when using mf with `bundle: false`', async () => {
105-
const fixturePath = path.resolve(__dirname, 'mf-bundle-false');
106-
const build = buildAndGetResults({
107-
fixturePath,
108-
});
104+
test("throw error when using mf with `bundle: false`", async () => {
105+
const fixturePath = path.resolve(__dirname, "mf-bundle-false");
106+
const build = buildAndGetResults({
107+
fixturePath,
108+
});
109109

110-
await expect(build).rejects.toThrowErrorMatchingInlineSnapshot(
111-
`[Error: When using "mf" format, "bundle" must be set to "true". Since the default value for "bundle" is "true", so you can either explicitly set it to "true" or remove the field entirely.]`,
112-
);
110+
await expect(build).rejects.toThrowErrorMatchingInlineSnapshot(
111+
`[Error: When using "mf" format, "bundle" must be set to "true". Since the default value for "bundle" is "true", so you can either explicitly set it to "true" or remove the field entirely.]`,
112+
);
113113
});
114114

115115
test("API plugin's api should be skipped in parser", async () => {
116-
const fixturePath = path.resolve(__dirname, 'api-plugin');
117-
const { entries } = await buildAndGetResults({
118-
fixturePath,
119-
});
116+
const fixturePath = path.resolve(__dirname, "api-plugin");
117+
const { entries } = await buildAndGetResults({
118+
fixturePath,
119+
});
120120

121-
expect(entries.esm).toMatchInlineSnapshot(`
122-
"const c = require.cache;
123-
export { c };
124-
"
125-
`);
121+
expect(entries.esm).toMatchInlineSnapshot(`
122+
"const a = require.cache;
123+
const b = require.extensions;
124+
const c = require.config;
125+
const d = require.version;
126+
const e = require.include;
127+
const f = require.onError;
128+
export { a, b, c, d, e, f };
129+
"
130+
`);
126131

127-
expect(entries.cjs).toContain('const c = require.cache;');
132+
expect(entries.cjs).toContain("const a = require.cache;");
133+
expect(entries.cjs).toContain("const b = require.extensions;");
134+
expect(entries.cjs).toContain("const c = require.config;");
135+
expect(entries.cjs).toContain("const d = require.version;");
136+
expect(entries.cjs).toContain("const e = require.include;");
137+
expect(entries.cjs).toContain("const f = require.onError;");
128138
});

0 commit comments

Comments
 (0)