Skip to content

Commit 78df839

Browse files
committed
test: add tests for original content and plugin context
- Add test to verify original content is preserved - Add test to check plugin context availability
1 parent 88a1642 commit 78df839

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

test/index.test.mjs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import test from 'ava';
22
import path from 'node:path';
33
import { fileURLToPath } from 'node:url';
4+
import { readFile } from 'node:fs/promises';
45
import { rollup } from 'rollup';
56
import { customImport } from '../dist/index.mjs';
67
import { execESM } from './helpers/eval.mjs';
@@ -77,3 +78,64 @@ test('throw error when importing a not existing file', async (t) => {
7778
}
7879
t.fail();
7980
});
81+
82+
test('original content', async (t) => {
83+
let originalContent;
84+
const bundle = await rollup({
85+
input: path.resolve(__dirname, 'fixtures/existing.js'),
86+
plugins: [
87+
customImport({
88+
include: ['**/file.js'],
89+
content: (id, original) => {
90+
originalContent = original;
91+
return {
92+
code: original,
93+
};
94+
},
95+
}),
96+
],
97+
});
98+
const realContent = await readFile(
99+
path.resolve(__dirname, 'fixtures/file.js'),
100+
'utf-8'
101+
);
102+
t.is(originalContent, realContent);
103+
});
104+
105+
const pluginContextKeys = [
106+
'addWatchFile',
107+
'cache',
108+
'debug',
109+
'emitFile',
110+
'error',
111+
'getFileName',
112+
'getModuleIds',
113+
'getModuleInfo',
114+
'getWatchFiles',
115+
'info',
116+
'load',
117+
'meta',
118+
'parse',
119+
'resolve',
120+
'setAssetSource',
121+
'warn',
122+
'getCombinedSourcemap',
123+
];
124+
test('plugin context', async (t) => {
125+
let keys;
126+
const bundle = await rollup({
127+
input: path.resolve(__dirname, 'fixtures/existing.js'),
128+
plugins: [
129+
customImport({
130+
include: ['**/file.js'],
131+
content(id, original) {
132+
keys = Object.keys(this);
133+
return {
134+
code: original,
135+
};
136+
},
137+
}),
138+
],
139+
});
140+
t.deepEqual(keys, pluginContextKeys);
141+
});

0 commit comments

Comments
 (0)