From ba6704607b4ee5a238fcb23ab3dffe1d52f0391f Mon Sep 17 00:00:00 2001 From: Joel Mut <62260472+sw-joelmut@users.noreply.github.com> Date: Fri, 22 Nov 2024 11:40:41 -0300 Subject: [PATCH] Refactor test:consumer script to use Mocha for better formatting and retries in case of failure (#4793) --- testing/consumer-test/.gitignore | 1 + testing/consumer-test/generateTests.ts | 32 +++++++++++ testing/consumer-test/package.json | 10 ++-- testing/consumer-test/run.ts | 65 ----------------------- testing/consumer-test/test.ts | 5 ++ testing/consumer-test/tests/typescript.js | 20 +++++++ 6 files changed, 63 insertions(+), 70 deletions(-) create mode 100644 testing/consumer-test/.gitignore create mode 100644 testing/consumer-test/generateTests.ts delete mode 100644 testing/consumer-test/run.ts create mode 100644 testing/consumer-test/tests/typescript.js diff --git a/testing/consumer-test/.gitignore b/testing/consumer-test/.gitignore new file mode 100644 index 0000000000..e67c65a344 --- /dev/null +++ b/testing/consumer-test/.gitignore @@ -0,0 +1 @@ +tests/generated/* \ No newline at end of file diff --git a/testing/consumer-test/generateTests.ts b/testing/consumer-test/generateTests.ts new file mode 100644 index 0000000000..5c6c4d51d4 --- /dev/null +++ b/testing/consumer-test/generateTests.ts @@ -0,0 +1,32 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. + */ + +/** + * This script generates test files for each version of TypeScript, so the test can run in parallel. + */ + +import fs from 'fs'; +import path from 'path'; + +const versions = ['4.7', '4.8', '4.9', '5.0', '5.1', '5.2', '5.3', '5.4', '5.5', '5.6']; +const targets = ['es6', 'esnext']; + +const testsDir = path.resolve(__dirname, 'tests/generated'); + +if (fs.existsSync(testsDir)) { + fs.rmSync(testsDir, { recursive: true }); +} + +fs.mkdirSync(testsDir); + +for (const version of versions) { + fs.writeFileSync( + path.resolve(testsDir, `typescript.${version}.test.js`), + ` +const testVersion = require('../typescript'); +testVersion('${version}', ['${targets.join("','")}']); + `, + ); +} diff --git a/testing/consumer-test/package.json b/testing/consumer-test/package.json index a54057d0b7..70023e1613 100644 --- a/testing/consumer-test/package.json +++ b/testing/consumer-test/package.json @@ -4,7 +4,9 @@ "version": "1.0.0", "scripts": { "lint": "eslint .", - "test": "ts-node run.ts --verbose" + "test": "npm-run-all test:gen test:mocha", + "test:gen": "ts-node ./generateTests.ts", + "test:mocha": "mocha ./tests/generated/*.test.js --parallel" }, "dependencies": { "adaptive-expressions": "4.1.6", @@ -35,11 +37,9 @@ "botframework-connector": "4.1.6", "botframework-schema": "4.1.6", "botframework-streaming": "4.1.6", - "eslint-plugin-only-warn": "^1.1.0", - "minimist": "^1.2.6", - "p-map": "^4.0.0" + "eslint-plugin-only-warn": "^1.1.0" }, "devDependencies": { - "@types/minimist": "^1.2.5" + "mocha": "^10.7.3" } } diff --git a/testing/consumer-test/run.ts b/testing/consumer-test/run.ts deleted file mode 100644 index 52a915a3ff..0000000000 --- a/testing/consumer-test/run.ts +++ /dev/null @@ -1,65 +0,0 @@ -import minimist from 'minimist'; -import pmap from 'p-map'; -import { exec } from 'child_process'; -import { promisify } from 'util'; - -const execp = promisify(exec); - -const versions = ['4.7', '4.8', '4.9', '5.0', '5.1', '5.2', '5.3', '5.4', '5.5', '5.6']; - -(async () => { - const flags = minimist(process.argv.slice(2), { - boolean: ['verbose'], - alias: { v: 'verbose' }, - }); - - try { - const [minTarget, maxTarget] = ['es6', 'esnext']; - console.log( - `Running typescript consumer test against ["${versions.join( - '", "' - )}"] with '${minTarget}' and '${maxTarget}' targets.` - ); - - const results = await pmap( - versions, - (version) => - Promise.all([ - execp(`npx -p typescript@${version} tsc -p tsconfig-test.json --target ${minTarget}`), - execp(`npx -p typescript@${version} tsc -p tsconfig-test.json --target ${maxTarget}`), - ]) - .then(() => ({ err: null, version, success: true })) - .catch((err) => ({ err, version, success: false })), - { concurrency: 2 } - ); - - const initialValue: { success: string[]; failed: string[] } = { - success: [], - failed: [], - }; - - const { success, failed } = results.reduce((acc, result) => { - if (result.success) { - acc.success.push(result.version); - } else { - acc.failed.push(result.version); - } - - return acc; - }, initialValue); - - console.log('Success:', success); - console.log('Failed:', failed); - - if (flags.verbose) { - results - .filter(({ success }) => success === false) - .forEach(({ err, version }) => console.error(`typescript@${version}`, err)); - } - - process.exit(failed.length ? 1 : 0); - } catch (error) { - console.error(error); - process.exit(-1); - } -})(); diff --git a/testing/consumer-test/test.ts b/testing/consumer-test/test.ts index 94865313cc..0620f330e5 100644 --- a/testing/consumer-test/test.ts +++ b/testing/consumer-test/test.ts @@ -1,3 +1,8 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. + */ + import * as adaptiveExpressions from 'adaptive-expressions'; import * as adaptiveExpressionsIE11 from 'adaptive-expressions-ie11'; import * as botbuilder from 'botbuilder'; diff --git a/testing/consumer-test/tests/typescript.js b/testing/consumer-test/tests/typescript.js new file mode 100644 index 0000000000..f74debc1b8 --- /dev/null +++ b/testing/consumer-test/tests/typescript.js @@ -0,0 +1,20 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. + */ + +const { exec } = require('child_process'); +const { promisify } = require('util'); +const execp = promisify(exec); + +module.exports = function testVersion(version, targets) { + describe(`typescript:${version}`, function () { + this.timeout(300000); // 5 minutes + this.retries(1); + for (const target of targets) { + it(`target:${target}`, async function () { + await execp(`npx -p typescript@${version} tsc -p tsconfig-test.json --target ${target}`); + }); + } + }); +};