Skip to content

Commit 044a94e

Browse files
feat: bundle expect from @playwright/test library (#812)
* feat: bundle expect from pw/test library * remove jest expect * bundle expect library correctly * fix build script * fix lint * add expect tests * throw error for non supported assertions * update * test and improve err msg --------- Co-authored-by: shahzad31 <[email protected]>
1 parent f585818 commit 044a94e

File tree

10 files changed

+409
-224
lines changed

10 files changed

+409
-224
lines changed

__tests__/cli.test.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,12 +323,27 @@ describe('CLI', () => {
323323
// flag turns on type checking
324324
process.env['TS_NODE_TYPE_CHECK'] = 'true';
325325
const cli = new CLIMock()
326-
.args([join(FIXTURES_DIR, 'expect.journey.ts')])
326+
.args([
327+
join(FIXTURES_DIR, 'expect.journey.ts'),
328+
'--match',
329+
'expect extends',
330+
])
327331
.run();
328332
expect(await cli.exitCode).toBe(0);
329333
process.env['TS_NODE_TYPE_CHECK'] = 'false';
330334
});
331335

336+
it('error on unsupported expect assertions', async () => {
337+
const cli = new CLIMock()
338+
.args([
339+
join(FIXTURES_DIR, 'expect.journey.ts'),
340+
'--match',
341+
'expect unsupported',
342+
])
343+
.run();
344+
expect(await cli.exitCode).toBe(1);
345+
});
346+
332347
describe('TLS site with self-signed cert', () => {
333348
let tlsServer: Server;
334349
let cliArgs: Array<string>;

__tests__/fixtures/expect.journey.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ declare global {
3333
}
3434
}
3535

36+
// Declare the global matcher in the PW namesapce to be used in tests
37+
// to satisfy the type checker and IDE
38+
declare global {
39+
namespace PlaywrightTest {
40+
interface Matchers<R> {
41+
toBeWithinRange(a: number, b: number): R;
42+
}
43+
}
44+
}
45+
3646
expect.extend({
3747
toBeWithinRange(received, floor, ceiling) {
3848
const pass = received >= floor && received <= ceiling;
@@ -52,7 +62,7 @@ expect.extend({
5262
},
5363
});
5464

55-
journey('expect journey', ({}) => {
65+
journey('expect extends', ({ page }) => {
5666
step('exports work', () => {
5767
expect(100).toBe(100);
5868
expect([1, 2, 3]).toEqual(expect.arrayContaining([1, 2, 3]));
@@ -63,4 +73,15 @@ journey('expect journey', ({}) => {
6373
expect(100).toBeWithinRange(90, 120);
6474
expect(101).not.toBeWithinRange(0, 100);
6575
});
76+
77+
step('pw test methods work', async () => {
78+
await expect(page).toHaveURL('about:blank');
79+
await expect(page).toHaveTitle('', { timeout: 100 });
80+
});
81+
});
82+
83+
journey('expect unsupported', ({ page }) => {
84+
step('throw unsupported', async () => {
85+
await expect(page).toHaveScreenshot();
86+
});
6687
});

bundles/build.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* MIT License
3+
*
4+
* Copyright (c) 2020-present, Elastic NV
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*
24+
*/
25+
26+
/* eslint-disable @typescript-eslint/no-var-requires */
27+
const path = require('path');
28+
const esbuild = require('esbuild');
29+
const { ENTRY_POINTS } = require('./src');
30+
31+
const outdir = path.join(__dirname, '..', 'dist', 'bundles');
32+
33+
(async () => {
34+
const ctx = await esbuild.context({
35+
entryPoints: ENTRY_POINTS,
36+
bundle: true,
37+
external: ['playwright-core', '@playwright/test/lib/transform/esmLoader'],
38+
outfile: path.join(outdir, 'lib', 'index.js'),
39+
format: 'cjs',
40+
platform: 'node',
41+
target: `node${process.version.slice(1)}`,
42+
minify: process.argv.includes('--minify'),
43+
sourcemap: process.argv.includes('--sourcemap'),
44+
sourcesContent: false,
45+
});
46+
await ctx.rebuild();
47+
await ctx.dispose();
48+
})().catch(error => {
49+
console.error(error);
50+
process.exit(1);
51+
});

bundles/package-lock.json

Lines changed: 62 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bundles/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "pkg-bundles",
3+
"version": "0.0.0",
4+
"private": true,
5+
"description": "Package bundles that is required by Synthetics",
6+
"scripts": {
7+
"bundle": "node build.js",
8+
"build": "npm run bundle -- --minify"
9+
},
10+
"dependencies": {
11+
"@playwright/test": "=1.37.0"
12+
}
13+
}

bundles/src/index.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* MIT License
3+
*
4+
* Copyright (c) 2020-present, Elastic NV
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*
24+
*/
25+
26+
/* eslint-disable @typescript-eslint/no-var-requires */
27+
const path = require('path');
28+
29+
module.exports.ENTRY_POINTS = [
30+
path.resolve('node_modules/@playwright/test/lib/matchers/expect.js'),
31+
];

0 commit comments

Comments
 (0)