Skip to content

Commit 056f7b9

Browse files
committed
✅ fix spy target
1 parent 47ef7d1 commit 056f7b9

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

test/integration/load-headers/index.test.ts

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import path from 'node:path';
2+
13
import { HeadersProps, UserscriptPlugin } from 'webpack-userscript';
4+
import * as fs from 'webpack-userscript/fs';
25

36
import { compile, watchCompile } from '../util';
47
import { Volume } from '../volume';
@@ -77,13 +80,11 @@ describe('load-headers', () => {
7780
headers: './headers.json',
7881
});
7982

80-
const loadFromHeadersFile = jest.spyOn(
81-
plugin.features[0],
82-
'loadFromHeadersFile' as any,
83-
);
83+
const readJSONSpy = jest.spyOn(fs, 'readJSON');
8484

8585
const entry = './entry.js';
8686
let step = 0;
87+
let inputFullPath = '';
8788

8889
await watchCompile(
8990
input,
@@ -93,16 +94,16 @@ describe('load-headers', () => {
9394
entry,
9495
plugins: [plugin],
9596
},
96-
async ({ output, writeFile }) => {
97+
async ({ output, writeFile, cwd }) => {
9798
switch (++step) {
9899
case 1:
99-
await writeFile(entry, Fixtures.entryJs);
100100
expect(output.toJSON()).toEqual({
101101
'/dist/output.user.js': Fixtures.entryUserJs(
102102
Fixtures.loadHeadersHeaders,
103103
),
104104
'/dist/output.meta.js': Fixtures.loadHeadersHeaders,
105105
});
106+
await writeFile(entry, Fixtures.entryJs);
106107
break;
107108

108109
case 2:
@@ -112,6 +113,8 @@ describe('load-headers', () => {
112113
fail('invalid steps');
113114
}
114115

116+
inputFullPath = cwd;
117+
115118
return step < 2;
116119
},
117120
);
@@ -120,7 +123,17 @@ describe('load-headers', () => {
120123
fail('invalid steps');
121124
}
122125

123-
expect(loadFromHeadersFile).toHaveBeenCalledOnce();
126+
const headersJsonPath = path.join(inputFullPath, 'headers.json');
127+
128+
// headers.json has only been read once
129+
expect(
130+
readJSONSpy.mock.calls.reduce(
131+
(count, call) => (call[0] === headersJsonPath ? ++count : count),
132+
0,
133+
),
134+
).toEqual(1);
135+
136+
readJSONSpy.mockRestore();
124137
});
125138
});
126139

@@ -189,4 +202,31 @@ describe('load-headers', () => {
189202
});
190203
});
191204
});
205+
206+
describe('i18n', () => {
207+
it('headers object', async () => {
208+
const output = await compile(input, {
209+
...Fixtures.webpackConfig,
210+
plugins: [
211+
new UserscriptPlugin({
212+
headers: {
213+
name: 'load-headers',
214+
},
215+
i18n: {
216+
en: {
217+
name: 'load-headers-en',
218+
},
219+
},
220+
}),
221+
],
222+
});
223+
224+
expect(output.toJSON()).toEqual({
225+
'/dist/output.user.js': Fixtures.entryUserJs(
226+
Fixtures.loadHeadersHeaders,
227+
),
228+
'/dist/output.meta.js': Fixtures.loadHeadersHeaders,
229+
});
230+
});
231+
});
192232
});

test/integration/util.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export async function compile(
4848
}
4949

5050
export interface WatchStep {
51+
cwd: string;
5152
output: Volume;
5253
writeFile: (
5354
file: string,
@@ -111,6 +112,7 @@ export async function watchCompile(
111112

112113
try {
113114
const conti = await handle({
115+
cwd: watchDir,
114116
output,
115117
writeFile: writeFileInWatchDir,
116118
});

0 commit comments

Comments
 (0)