Skip to content

Commit

Permalink
Add tests for plugin config hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister committed Mar 27, 2021
1 parent 0db05e5 commit 3419af9
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/tall-moons-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'wmr': patch
---

Add plugin `config()` and `configResolved()` tests
8 changes: 8 additions & 0 deletions packages/wmr/test/fixtures.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,14 @@ describe('fixtures', () => {
expect(text).toEqual('file pre1 pre2 normal1 normal2 post1 post2');
});

it('should support config() and configResolved() hooks', async () => {
await loadFixture('plugin-config', env);
instance = await runWmrFast(env.tmp.path);
await env.page.goto(await instance.address);
expect(await env.page.evaluate(`fetch('/test').then(r => r.text())`)).toEqual('it works');
expect(await env.page.evaluate(`fetch('/test-resolved').then(r => r.text())`)).toEqual('it works');
});

it('should add middlewares via config', async () => {
await loadFixture('plugin-middlewares', env);
instance = await runWmrFast(env.tmp.path);
Expand Down
1 change: 1 addition & 0 deletions packages/wmr/test/fixtures/plugin-config/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>Nothing to see here!</p>
38 changes: 38 additions & 0 deletions packages/wmr/test/fixtures/plugin-config/wmr.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export default function foo() {
return [
{
name: 'no-return',
async config() {},
configResolved() {}
},
{
name: 'with-return',
config() {
return {
middleware: [
(req, res, next) => {
if (req.url === '/test') {
res.end('it works');
} else {
next();
}
}
]
};
},
configResolved() {
return {
middleware: [
(req, res, next) => {
if (req.url === '/test-resolved') {
res.end('it works');
} else {
next();
}
}
]
};
}
}
];
}
4 changes: 2 additions & 2 deletions packages/wmr/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ declare module 'wmr' {
* Specify when a plugin should be executed.
*/
enforce?: 'pre' | 'post' | 'normal';
config?: (config: Options) => Partial<Options>;
configResolved?: (config: Options) => Partial<Options>;
config?: (config: Options) => Partial<Options> | void;
configResolved?: (config: Options) => Partial<Options> | void;
}

export interface Options {
Expand Down

0 comments on commit 3419af9

Please sign in to comment.