2021-06-07 03:09:53 +03:00
|
|
|
/**
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-07-05 20:46:30 +03:00
|
|
|
import type { JSONReport, JSONReportSuite, JSONReportTest, JSONReportTestResult } from '@playwright/test/reporter';
|
2021-06-07 03:09:53 +03:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as os from 'os';
|
2021-10-15 01:48:05 +03:00
|
|
|
import * as path from 'path';
|
2022-04-19 06:20:49 +03:00
|
|
|
import { rimraf, PNG } from 'playwright-core/lib/utilsBundle';
|
2021-06-07 03:09:53 +03:00
|
|
|
import { promisify } from 'util';
|
2022-04-07 00:57:14 +03:00
|
|
|
import type { CommonFixtures } from '../config/commonFixtures';
|
|
|
|
import { commonFixtures } from '../config/commonFixtures';
|
|
|
|
import type { ServerFixtures, ServerWorkerOptions } from '../config/serverFixtures';
|
|
|
|
import { serverFixtures } from '../config/serverFixtures';
|
|
|
|
import type { TestInfo } from './stable-test-runner';
|
2022-07-05 20:46:30 +03:00
|
|
|
import { expect } from './stable-test-runner';
|
2022-04-07 00:57:14 +03:00
|
|
|
import { test as base } from './stable-test-runner';
|
2021-06-07 03:09:53 +03:00
|
|
|
|
|
|
|
const removeFolderAsync = promisify(rimraf);
|
|
|
|
|
2022-07-05 20:46:30 +03:00
|
|
|
export type RunResult = {
|
2021-06-07 03:09:53 +03:00
|
|
|
exitCode: number,
|
|
|
|
output: string,
|
|
|
|
passed: number,
|
|
|
|
failed: number,
|
|
|
|
flaky: number,
|
|
|
|
skipped: number,
|
2022-08-02 22:55:43 +03:00
|
|
|
interrupted: number,
|
2021-07-09 03:16:36 +03:00
|
|
|
report: JSONReport,
|
2021-06-07 03:09:53 +03:00
|
|
|
results: any[],
|
|
|
|
};
|
|
|
|
|
|
|
|
type TSCResult = {
|
|
|
|
output: string;
|
|
|
|
exitCode: number;
|
|
|
|
};
|
|
|
|
|
|
|
|
type Files = { [key: string]: string | Buffer };
|
|
|
|
type Params = { [key: string]: string | number | boolean | string[] };
|
|
|
|
type Env = { [key: string]: string | number | boolean | undefined };
|
|
|
|
|
|
|
|
async function writeFiles(testInfo: TestInfo, files: Files) {
|
|
|
|
const baseDir = testInfo.outputPath();
|
|
|
|
|
|
|
|
const headerJS = `
|
2021-10-11 17:52:17 +03:00
|
|
|
const pwt = require('@playwright/test');
|
2021-06-07 03:09:53 +03:00
|
|
|
`;
|
|
|
|
const headerTS = `
|
2021-10-11 17:52:17 +03:00
|
|
|
import * as pwt from '@playwright/test';
|
2021-06-07 03:09:53 +03:00
|
|
|
`;
|
2021-11-24 23:42:48 +03:00
|
|
|
const headerESM = `
|
2021-10-11 17:52:17 +03:00
|
|
|
import * as pwt from '@playwright/test';
|
2021-06-30 01:28:41 +03:00
|
|
|
`;
|
2021-06-07 03:09:53 +03:00
|
|
|
|
|
|
|
const hasConfig = Object.keys(files).some(name => name.includes('.config.'));
|
|
|
|
if (!hasConfig) {
|
|
|
|
files = {
|
|
|
|
...files,
|
|
|
|
'playwright.config.ts': `
|
|
|
|
module.exports = { projects: [ {} ] };
|
|
|
|
`,
|
|
|
|
};
|
|
|
|
}
|
2022-03-24 02:05:49 +03:00
|
|
|
if (!Object.keys(files).some(name => name.includes('package.json'))) {
|
|
|
|
files = {
|
|
|
|
...files,
|
|
|
|
'package.json': `{ "name": "test-project" }`,
|
|
|
|
};
|
|
|
|
}
|
2021-06-07 03:09:53 +03:00
|
|
|
|
|
|
|
await Promise.all(Object.keys(files).map(async name => {
|
|
|
|
const fullName = path.join(baseDir, name);
|
|
|
|
await fs.promises.mkdir(path.dirname(fullName), { recursive: true });
|
2021-06-30 01:28:41 +03:00
|
|
|
const isTypeScriptSourceFile = name.endsWith('.ts') && !name.endsWith('.d.ts');
|
2021-11-24 23:42:48 +03:00
|
|
|
const isJSModule = name.endsWith('.mjs') || name.includes('esm');
|
|
|
|
const header = isTypeScriptSourceFile ? headerTS : (isJSModule ? headerESM : headerJS);
|
2021-07-12 19:59:58 +03:00
|
|
|
if (typeof files[name] === 'string' && files[name].includes('//@no-header')) {
|
|
|
|
await fs.promises.writeFile(fullName, files[name]);
|
|
|
|
} else if (/(spec|test)\.(js|ts|mjs)$/.test(name)) {
|
2021-06-07 08:07:07 +03:00
|
|
|
const fileHeader = header + 'const { expect } = pwt;\n';
|
2021-06-07 03:09:53 +03:00
|
|
|
await fs.promises.writeFile(fullName, fileHeader + files[name]);
|
|
|
|
} else if (/\.(js|ts)$/.test(name) && !name.endsWith('d.ts')) {
|
|
|
|
await fs.promises.writeFile(fullName, header + files[name]);
|
|
|
|
} else {
|
|
|
|
await fs.promises.writeFile(fullName, files[name]);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
return baseDir;
|
|
|
|
}
|
|
|
|
|
2021-10-11 22:07:40 +03:00
|
|
|
const cliEntrypoint = path.join(__dirname, '../../packages/playwright-core/cli.js');
|
|
|
|
|
2021-09-22 02:24:48 +03:00
|
|
|
async function runPlaywrightTest(childProcess: CommonFixtures['childProcess'], baseDir: string, params: any, env: Env, options: RunOptions): Promise<RunResult> {
|
2022-06-19 01:47:26 +03:00
|
|
|
const paramList: string[] = [];
|
2021-06-07 03:09:53 +03:00
|
|
|
for (const key of Object.keys(params)) {
|
|
|
|
for (const value of Array.isArray(params[key]) ? params[key] : [params[key]]) {
|
|
|
|
const k = key.startsWith('-') ? key : '--' + key;
|
|
|
|
paramList.push(params[key] === true ? `${k}` : `${k}=${value}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const outputDir = path.join(baseDir, 'test-results');
|
|
|
|
const reportFile = path.join(outputDir, 'report.json');
|
2021-10-11 22:07:40 +03:00
|
|
|
const args = ['node', cliEntrypoint, 'test'];
|
2021-09-21 03:17:12 +03:00
|
|
|
if (!options.usesCustomOutputDir)
|
2021-06-23 12:08:35 +03:00
|
|
|
args.push('--output=' + outputDir);
|
2022-06-19 01:47:26 +03:00
|
|
|
if (!options.usesCustomReporters)
|
|
|
|
args.push('--reporter=dot,json');
|
2021-06-07 03:09:53 +03:00
|
|
|
args.push(
|
|
|
|
'--workers=2',
|
|
|
|
...paramList
|
|
|
|
);
|
2021-09-21 03:17:12 +03:00
|
|
|
if (options.additionalArgs)
|
|
|
|
args.push(...options.additionalArgs);
|
2021-06-07 03:09:53 +03:00
|
|
|
const cacheDir = fs.mkdtempSync(path.join(os.tmpdir(), 'playwright-test-cache-'));
|
2021-09-22 02:24:48 +03:00
|
|
|
const testProcess = childProcess({
|
2021-09-21 03:17:12 +03:00
|
|
|
command: args,
|
2021-06-07 03:09:53 +03:00
|
|
|
env: {
|
|
|
|
...process.env,
|
|
|
|
PLAYWRIGHT_JSON_OUTPUT_NAME: reportFile,
|
|
|
|
PWTEST_CACHE_DIR: cacheDir,
|
2022-03-23 02:28:04 +03:00
|
|
|
// BEGIN: Reserved CI
|
2022-02-24 23:39:28 +03:00
|
|
|
CI: undefined,
|
2022-03-23 02:28:04 +03:00
|
|
|
BUILD_URL: undefined,
|
|
|
|
CI_COMMIT_SHA: undefined,
|
|
|
|
CI_JOB_URL: undefined,
|
|
|
|
CI_PROJECT_URL: undefined,
|
|
|
|
GITHUB_REPOSITORY: undefined,
|
|
|
|
GITHUB_RUN_ID: undefined,
|
|
|
|
GITHUB_SERVER_URL: undefined,
|
|
|
|
GITHUB_SHA: undefined,
|
|
|
|
// END: Reserved CI
|
2022-02-24 23:39:28 +03:00
|
|
|
PW_TEST_HTML_REPORT_OPEN: undefined,
|
2022-02-08 20:22:45 +03:00
|
|
|
PW_TEST_REPORTER: undefined,
|
2022-02-02 04:15:55 +03:00
|
|
|
PW_TEST_REPORTER_WS_ENDPOINT: undefined,
|
|
|
|
PW_TEST_SOURCE_TRANSFORM: undefined,
|
2022-02-09 18:14:11 +03:00
|
|
|
PW_TEST_SOURCE_TRANSFORM_SCOPE: undefined,
|
2022-02-02 04:15:55 +03:00
|
|
|
PW_OUT_OF_PROCESS_DRIVER: undefined,
|
|
|
|
NODE_OPTIONS: undefined,
|
2022-02-24 23:39:28 +03:00
|
|
|
...env,
|
2021-06-07 03:09:53 +03:00
|
|
|
},
|
2022-03-24 02:05:49 +03:00
|
|
|
cwd: options.cwd ? path.resolve(baseDir, options.cwd) : baseDir,
|
2021-06-07 03:09:53 +03:00
|
|
|
});
|
2021-09-22 02:24:48 +03:00
|
|
|
let didSendSigint = false;
|
|
|
|
testProcess.onOutput = () => {
|
|
|
|
if (options.sendSIGINTAfter && !didSendSigint && countTimes(testProcess.output, '%%SEND-SIGINT%%') >= options.sendSIGINTAfter) {
|
|
|
|
didSendSigint = true;
|
2022-08-02 22:55:43 +03:00
|
|
|
process.kill(testProcess.process.pid!, 'SIGINT');
|
2021-09-22 02:24:48 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
const { exitCode } = await testProcess.exited;
|
2021-06-07 03:09:53 +03:00
|
|
|
await removeFolderAsync(cacheDir);
|
|
|
|
|
2021-09-21 03:17:12 +03:00
|
|
|
const outputString = testProcess.output.toString();
|
2021-06-07 03:09:53 +03:00
|
|
|
const summary = (re: RegExp) => {
|
|
|
|
let result = 0;
|
|
|
|
let match = re.exec(outputString);
|
|
|
|
while (match) {
|
|
|
|
result += (+match[1]);
|
|
|
|
match = re.exec(outputString);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
const passed = summary(/(\d+) passed/g);
|
|
|
|
const failed = summary(/(\d+) failed/g);
|
|
|
|
const flaky = summary(/(\d+) flaky/g);
|
|
|
|
const skipped = summary(/(\d+) skipped/g);
|
2022-08-02 22:55:43 +03:00
|
|
|
const interrupted = summary(/(\d+) interrupted/g);
|
2021-06-07 03:09:53 +03:00
|
|
|
let report;
|
|
|
|
try {
|
|
|
|
report = JSON.parse(fs.readFileSync(reportFile).toString());
|
|
|
|
} catch (e) {
|
2021-09-21 03:17:12 +03:00
|
|
|
testProcess.output += '\n' + e.toString();
|
2021-06-07 03:09:53 +03:00
|
|
|
}
|
|
|
|
|
2022-06-19 01:47:26 +03:00
|
|
|
const results: JSONReportTestResult[] = [];
|
2021-07-09 03:16:36 +03:00
|
|
|
function visitSuites(suites?: JSONReportSuite[]) {
|
2021-06-07 03:09:53 +03:00
|
|
|
if (!suites)
|
|
|
|
return;
|
|
|
|
for (const suite of suites) {
|
|
|
|
for (const spec of suite.specs) {
|
|
|
|
for (const test of spec.tests)
|
|
|
|
results.push(...test.results);
|
|
|
|
}
|
|
|
|
visitSuites(suite.suites);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (report)
|
|
|
|
visitSuites(report.suites);
|
|
|
|
|
|
|
|
return {
|
2021-09-21 03:17:12 +03:00
|
|
|
exitCode,
|
|
|
|
output: testProcess.output,
|
2021-06-07 03:09:53 +03:00
|
|
|
passed,
|
|
|
|
failed,
|
|
|
|
flaky,
|
|
|
|
skipped,
|
2022-08-02 22:55:43 +03:00
|
|
|
interrupted,
|
2021-06-07 03:09:53 +03:00
|
|
|
report,
|
|
|
|
results,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-07-07 22:04:43 +03:00
|
|
|
type RunOptions = {
|
|
|
|
sendSIGINTAfter?: number;
|
2021-09-21 03:17:12 +03:00
|
|
|
usesCustomOutputDir?: boolean;
|
2022-06-19 01:47:26 +03:00
|
|
|
usesCustomReporters?: boolean;
|
2021-09-21 03:17:12 +03:00
|
|
|
additionalArgs?: string[];
|
2022-03-24 02:05:49 +03:00
|
|
|
cwd?: string,
|
2021-07-07 22:04:43 +03:00
|
|
|
};
|
2021-06-07 03:09:53 +03:00
|
|
|
type Fixtures = {
|
|
|
|
writeFiles: (files: Files) => Promise<string>;
|
2022-03-23 02:28:04 +03:00
|
|
|
runInlineTest: (files: Files, params?: Params, env?: Env, options?: RunOptions, beforeRunPlaywrightTest?: ({ baseDir }: { baseDir: string }) => Promise<void>) => Promise<RunResult>;
|
2021-06-07 03:09:53 +03:00
|
|
|
runTSC: (files: Files) => Promise<TSCResult>;
|
2022-06-02 02:50:23 +03:00
|
|
|
nodeVersion: { major: number, minor: number, patch: number },
|
2021-06-07 03:09:53 +03:00
|
|
|
};
|
|
|
|
|
feat(test runner): replace declare/define with "options" (#10293)
1. Fixtures defined in test.extend() can now have `{ option: true }` configuration that makes them overridable in the config. Options support all other properties of fixtures - value/function, scope, auto.
```
const test = base.extend<MyOptions>({
foo: ['default', { option: true }],
});
```
2. test.declare() and project.define are removed.
3. project.use applies overrides to default option values and nothing else. Any test.extend() and test.use() calls take priority over config options.
Required user changes: if someone used to define fixture options with test.extend(), overriding them in config will stop working. The solution is to add `{ option: true }`.
```
// Old code
export const test = base.extend<{ myOption: number, myFixture: number }>({
myOption: 123,
myFixture: ({ myOption }, use) => use(2 * myOption),
});
// New code
export const test = base.extend<{ myOption: number, myFixture: number }>({
myOption: [123, { option: true }],
myFixture: ({ myOption }, use) => use(2 * myOption),
});
```
2021-11-19 02:45:52 +03:00
|
|
|
export const test = base
|
2021-12-18 21:12:35 +03:00
|
|
|
.extend<CommonFixtures>(commonFixtures)
|
2022-01-06 20:29:05 +03:00
|
|
|
.extend<ServerFixtures, ServerWorkerOptions>(serverFixtures)
|
feat(test runner): replace declare/define with "options" (#10293)
1. Fixtures defined in test.extend() can now have `{ option: true }` configuration that makes them overridable in the config. Options support all other properties of fixtures - value/function, scope, auto.
```
const test = base.extend<MyOptions>({
foo: ['default', { option: true }],
});
```
2. test.declare() and project.define are removed.
3. project.use applies overrides to default option values and nothing else. Any test.extend() and test.use() calls take priority over config options.
Required user changes: if someone used to define fixture options with test.extend(), overriding them in config will stop working. The solution is to add `{ option: true }`.
```
// Old code
export const test = base.extend<{ myOption: number, myFixture: number }>({
myOption: 123,
myFixture: ({ myOption }, use) => use(2 * myOption),
});
// New code
export const test = base.extend<{ myOption: number, myFixture: number }>({
myOption: [123, { option: true }],
myFixture: ({ myOption }, use) => use(2 * myOption),
});
```
2021-11-19 02:45:52 +03:00
|
|
|
.extend<Fixtures>({
|
|
|
|
writeFiles: async ({}, use, testInfo) => {
|
|
|
|
await use(files => writeFiles(testInfo, files));
|
|
|
|
},
|
2021-06-07 03:09:53 +03:00
|
|
|
|
2022-05-04 00:25:56 +03:00
|
|
|
runInlineTest: async ({ childProcess }, use, testInfo: TestInfo) => {
|
2022-03-23 02:28:04 +03:00
|
|
|
await use(async (files: Files, params: Params = {}, env: Env = {}, options: RunOptions = {}, beforeRunPlaywrightTest?: ({ baseDir: string }) => Promise<void>) => {
|
feat(test runner): replace declare/define with "options" (#10293)
1. Fixtures defined in test.extend() can now have `{ option: true }` configuration that makes them overridable in the config. Options support all other properties of fixtures - value/function, scope, auto.
```
const test = base.extend<MyOptions>({
foo: ['default', { option: true }],
});
```
2. test.declare() and project.define are removed.
3. project.use applies overrides to default option values and nothing else. Any test.extend() and test.use() calls take priority over config options.
Required user changes: if someone used to define fixture options with test.extend(), overriding them in config will stop working. The solution is to add `{ option: true }`.
```
// Old code
export const test = base.extend<{ myOption: number, myFixture: number }>({
myOption: 123,
myFixture: ({ myOption }, use) => use(2 * myOption),
});
// New code
export const test = base.extend<{ myOption: number, myFixture: number }>({
myOption: [123, { option: true }],
myFixture: ({ myOption }, use) => use(2 * myOption),
});
```
2021-11-19 02:45:52 +03:00
|
|
|
const baseDir = await writeFiles(testInfo, files);
|
2022-03-23 02:28:04 +03:00
|
|
|
if (beforeRunPlaywrightTest)
|
|
|
|
await beforeRunPlaywrightTest({ baseDir });
|
feat(test runner): replace declare/define with "options" (#10293)
1. Fixtures defined in test.extend() can now have `{ option: true }` configuration that makes them overridable in the config. Options support all other properties of fixtures - value/function, scope, auto.
```
const test = base.extend<MyOptions>({
foo: ['default', { option: true }],
});
```
2. test.declare() and project.define are removed.
3. project.use applies overrides to default option values and nothing else. Any test.extend() and test.use() calls take priority over config options.
Required user changes: if someone used to define fixture options with test.extend(), overriding them in config will stop working. The solution is to add `{ option: true }`.
```
// Old code
export const test = base.extend<{ myOption: number, myFixture: number }>({
myOption: 123,
myFixture: ({ myOption }, use) => use(2 * myOption),
});
// New code
export const test = base.extend<{ myOption: number, myFixture: number }>({
myOption: [123, { option: true }],
myFixture: ({ myOption }, use) => use(2 * myOption),
});
```
2021-11-19 02:45:52 +03:00
|
|
|
return await runPlaywrightTest(childProcess, baseDir, params, env, options);
|
|
|
|
});
|
|
|
|
},
|
2021-06-07 03:09:53 +03:00
|
|
|
|
feat(test runner): replace declare/define with "options" (#10293)
1. Fixtures defined in test.extend() can now have `{ option: true }` configuration that makes them overridable in the config. Options support all other properties of fixtures - value/function, scope, auto.
```
const test = base.extend<MyOptions>({
foo: ['default', { option: true }],
});
```
2. test.declare() and project.define are removed.
3. project.use applies overrides to default option values and nothing else. Any test.extend() and test.use() calls take priority over config options.
Required user changes: if someone used to define fixture options with test.extend(), overriding them in config will stop working. The solution is to add `{ option: true }`.
```
// Old code
export const test = base.extend<{ myOption: number, myFixture: number }>({
myOption: 123,
myFixture: ({ myOption }, use) => use(2 * myOption),
});
// New code
export const test = base.extend<{ myOption: number, myFixture: number }>({
myOption: [123, { option: true }],
myFixture: ({ myOption }, use) => use(2 * myOption),
});
```
2021-11-19 02:45:52 +03:00
|
|
|
runTSC: async ({ childProcess }, use, testInfo) => {
|
|
|
|
await use(async files => {
|
|
|
|
const baseDir = await writeFiles(testInfo, { 'tsconfig.json': JSON.stringify(TSCONFIG), ...files });
|
|
|
|
const tsc = childProcess({
|
|
|
|
command: ['npx', 'tsc', '-p', baseDir],
|
|
|
|
cwd: baseDir,
|
|
|
|
shell: true,
|
|
|
|
});
|
|
|
|
const { exitCode } = await tsc.exited;
|
|
|
|
return { exitCode, output: tsc.output };
|
|
|
|
});
|
|
|
|
},
|
2022-06-02 02:50:23 +03:00
|
|
|
|
|
|
|
nodeVersion: async ({}, use) => {
|
|
|
|
const [major, minor, patch] = process.versions.node.split('.');
|
|
|
|
await use({ major: +major, minor: +minor, patch: +patch });
|
|
|
|
},
|
2021-09-21 03:17:12 +03:00
|
|
|
});
|
2021-06-07 03:09:53 +03:00
|
|
|
|
|
|
|
const TSCONFIG = {
|
|
|
|
'compilerOptions': {
|
|
|
|
'target': 'ESNext',
|
|
|
|
'moduleResolution': 'node',
|
|
|
|
'module': 'commonjs',
|
|
|
|
'strict': true,
|
|
|
|
'esModuleInterop': true,
|
|
|
|
'allowSyntheticDefaultImports': true,
|
|
|
|
'rootDir': '.',
|
2022-03-16 23:34:41 +03:00
|
|
|
'lib': ['esnext', 'dom', 'DOM.Iterable'],
|
|
|
|
'noEmit': true,
|
2021-06-07 03:09:53 +03:00
|
|
|
},
|
|
|
|
'exclude': [
|
|
|
|
'node_modules'
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
2021-09-03 21:22:25 +03:00
|
|
|
export { expect } from './stable-test-runner';
|
2021-06-07 03:09:53 +03:00
|
|
|
|
|
|
|
const asciiRegex = new RegExp('[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))', 'g');
|
2022-02-01 04:14:59 +03:00
|
|
|
export function stripAnsi(str: string): string {
|
2021-06-07 03:09:53 +03:00
|
|
|
return str.replace(asciiRegex, '');
|
|
|
|
}
|
2021-07-07 22:04:43 +03:00
|
|
|
|
2022-01-13 21:38:47 +03:00
|
|
|
export function countTimes(s: string, sub: string): number {
|
2021-07-07 22:04:43 +03:00
|
|
|
let result = 0;
|
|
|
|
for (let index = 0; index !== -1;) {
|
|
|
|
index = s.indexOf(sub, index);
|
|
|
|
if (index !== -1) {
|
|
|
|
result++;
|
|
|
|
index += sub.length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2022-02-24 00:17:37 +03:00
|
|
|
|
2022-02-28 23:25:59 +03:00
|
|
|
export function createImage(width: number, height: number, r: number = 0, g: number = 0, b: number = 0, a: number = 255): Buffer {
|
2022-02-24 00:17:37 +03:00
|
|
|
const image = new PNG({ width, height });
|
|
|
|
// Make both images red.
|
|
|
|
for (let i = 0; i < width * height; ++i) {
|
|
|
|
image.data[i * 4 + 0] = r;
|
|
|
|
image.data[i * 4 + 1] = g;
|
|
|
|
image.data[i * 4 + 2] = b;
|
2022-02-28 23:25:59 +03:00
|
|
|
image.data[i * 4 + 3] = a;
|
2022-02-24 00:17:37 +03:00
|
|
|
}
|
|
|
|
return PNG.sync.write(image);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createWhiteImage(width: number, height: number) {
|
|
|
|
return createImage(width, height, 255, 255, 255);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function paintBlackPixels(image: Buffer, blackPixelsCount: number): Buffer {
|
2022-03-09 07:29:31 +03:00
|
|
|
const png = PNG.sync.read(image);
|
2022-02-24 00:17:37 +03:00
|
|
|
for (let i = 0; i < blackPixelsCount; ++i) {
|
|
|
|
for (let j = 0; j < 3; ++j)
|
2022-03-09 07:29:31 +03:00
|
|
|
png.data[i * 4 + j] = 0;
|
2022-02-24 00:17:37 +03:00
|
|
|
}
|
2022-03-09 07:29:31 +03:00
|
|
|
return PNG.sync.write(png);
|
2022-02-24 00:17:37 +03:00
|
|
|
}
|
2022-07-05 20:46:30 +03:00
|
|
|
|
|
|
|
export function allTests(result: RunResult) {
|
|
|
|
const tests: { title: string; expectedStatus: JSONReportTest['expectedStatus'], actualStatus: JSONReportTest['status'], annotations: string[] }[] = [];
|
|
|
|
const visit = (suite: JSONReportSuite) => {
|
|
|
|
for (const spec of suite.specs)
|
|
|
|
spec.tests.forEach(t => tests.push({ title: spec.title, expectedStatus: t.expectedStatus, actualStatus: t.status, annotations: t.annotations.map(a => a.type) }));
|
|
|
|
suite.suites?.forEach(s => visit(s));
|
|
|
|
};
|
|
|
|
visit(result.report.suites[0]);
|
|
|
|
return tests;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function expectTestHelper(result: RunResult) {
|
|
|
|
return (title: string, expectedStatus: string, status: string, annotations: any) => {
|
|
|
|
const tests = allTests(result).filter(t => t.title === title);
|
|
|
|
for (const test of tests) {
|
|
|
|
expect(test.expectedStatus, `title: ${title}`).toBe(expectedStatus);
|
|
|
|
expect(test.actualStatus, `title: ${title}`).toBe(status);
|
|
|
|
expect(test.annotations, `title: ${title}`).toEqual(annotations);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|