From d746ecbc241aa05815aab01b36ce185e7bba4583 Mon Sep 17 00:00:00 2001 From: Salam Suleymanov Date: Fri, 4 Oct 2024 16:58:26 +0200 Subject: [PATCH] add options to run all dtc test files in a project (#6) * add options to run all dts test files in a project * fix functions tests * fix playwrite * Create TestCaseExecution * Bump version * Update dependencies * Download playwright browser * Install playwright --------- Co-authored-by: Ab C <219340+abdala@users.noreply.github.com> --- .github/workflows/tests.yml | 3 + assert/package.json | 3 +- dtc-aws-plugin/package.json | 9 +- dtc-mysql-plugin/package.json | 7 +- dtc-mysql-plugin/test/mock-mysql.test.ts | 6 +- dtc-playwright-plugin/package.json | 7 +- dtc-playwright-plugin/src/PlaywrightRunner.ts | 11 ++- dtc-playwright-plugin/src/browser.spec.ts | 10 +-- .../test/PlaywrightRunner.test.ts | 11 ++- dtc-playwright-plugin/test/fixtures/config.js | 6 +- .../test/fixtures/{base.js => t1.dtc.js} | 0 dtc-playwright-plugin/test/fixtures/t2.dtc.js | 58 +++++++++++++ dtc/package.json | 6 +- dtc/src/cli.ts | 14 +-- dtc/src/config.ts | 85 +++++++++++++++---- dtc/src/domain.ts | 36 ++++---- dtc/src/index.ts | 27 ++++-- dtc/src/plugins/FunctionCallPlugin.ts | 5 +- dtc/src/runners/NodeTestRunner.ts | 8 +- dtc/test/cli.test.ts | 16 +++- dtc/test/fixtures/t1.dtc.ts | 13 +++ dtc/test/fixtures/t2.dtc.ts | 13 +++ dtc/test/fixtures/tests/t3.dtc.ts | 13 +++ dtc/test/fixtures/unit.js | 3 +- dtc/test/index.test.ts | 8 +- nock-aws/package.json | 3 +- package-lock.json | 39 +++++---- package.json | 3 +- yaml/package.json | 3 +- 29 files changed, 323 insertions(+), 103 deletions(-) rename dtc-playwright-plugin/test/fixtures/{base.js => t1.dtc.js} (100%) create mode 100644 dtc-playwright-plugin/test/fixtures/t2.dtc.js create mode 100644 dtc/test/fixtures/t1.dtc.ts create mode 100644 dtc/test/fixtures/t2.dtc.ts create mode 100644 dtc/test/fixtures/tests/t3.dtc.ts diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 9f6f1a4..7fdf4b8 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -22,5 +22,8 @@ jobs: with: node-version: ${{ matrix.node-version }} - run: npm ci + - run: | + npx playwright install + npx playwright install-deps chromium - run: npm run build - run: npm test diff --git a/assert/package.json b/assert/package.json index 5f37647..8dd4d83 100644 --- a/assert/package.json +++ b/assert/package.json @@ -1,6 +1,6 @@ { "name": "@cgauge/assert", - "version": "0.2.2", + "version": "0.3.0", "description": "Extra assert library", "type": "module", "repository": { @@ -20,6 +20,7 @@ "scripts": { "build": "rm -rf dist && tsc", "test": "tsx --test test/*.test.ts", + "test:only": "tsx --test --test-only test/*.test.ts", "test:coverage": "tsx --experimental-test-coverage --test test/*.test.ts" } } diff --git a/dtc-aws-plugin/package.json b/dtc-aws-plugin/package.json index 38532e4..f733e13 100644 --- a/dtc-aws-plugin/package.json +++ b/dtc-aws-plugin/package.json @@ -1,6 +1,6 @@ { "name": "@cgauge/dtc-aws-plugin", - "version": "0.2.2", + "version": "0.3.0", "description": "AWS plugin for Declarative TestCases", "repository": { "type": "git", @@ -15,9 +15,9 @@ "author": "Abdala Cerqueira", "license": "LGPL-3.0-or-later", "dependencies": { - "@cgauge/assert": "^0.2.0", - "@cgauge/dtc": "^0.2.0", - "@cgauge/nock-aws": "^0.2.0", + "@cgauge/assert": "^0.3.0", + "@cgauge/dtc": "^0.3.0", + "@cgauge/nock-aws": "^0.3.0", "@aws-sdk/client-dynamodb": "^3.645.0", "@aws-sdk/client-eventbridge": "^3.645.0", "@aws-sdk/client-lambda": "^3.645.0", @@ -28,6 +28,7 @@ "scripts": { "build": "rm -rf dist && tsc", "test": "AWS_MAX_ATTEMPTS=0 AWS_REGION=eu-west-1 AWS_ACCESS_KEY_ID=test AWS_SECRET_ACCESS_KEY=test tsx --test test/*.test.ts", + "test:only": "AWS_MAX_ATTEMPTS=0 AWS_REGION=eu-west-1 AWS_ACCESS_KEY_ID=test AWS_SECRET_ACCESS_KEY=test tsx --test --test-only test/*.test.ts", "test:coverage": "AWS_MAX_ATTEMPTS=0 AWS_REGION=eu-west-1 AWS_ACCESS_KEY_ID=test AWS_SECRET_ACCESS_KEY=test tsx --experimental-test-coverage --test test/*.test.ts" } } diff --git a/dtc-mysql-plugin/package.json b/dtc-mysql-plugin/package.json index 32a77bd..f3f0c8a 100644 --- a/dtc-mysql-plugin/package.json +++ b/dtc-mysql-plugin/package.json @@ -1,6 +1,6 @@ { "name": "@cgauge/dtc-mysql-plugin", - "version": "0.2.2", + "version": "0.3.0", "description": "MySQL plugin for Declarative TestCases", "repository": { "type": "git", @@ -15,14 +15,15 @@ "author": "Abdala Cerqueira", "license": "LGPL-3.0-or-later", "dependencies": { - "@cgauge/assert": "^0.2.0", - "@cgauge/dtc": "^0.2.0", + "@cgauge/assert": "^0.3.0", + "@cgauge/dtc": "^0.3.0", "mysql2": "^3.11.0", "node-sql-parser": "^5.1.0" }, "scripts": { "build": "rm -rf dist && tsc", "test": "tsx --test test/*.test.ts", + "test:only": "tsx --test --test-only test/*.test.ts", "test:coverage": "tsx --experimental-test-coverage --test test/*.test.ts" } } diff --git a/dtc-mysql-plugin/test/mock-mysql.test.ts b/dtc-mysql-plugin/test/mock-mysql.test.ts index e6ae1d3..307e65f 100644 --- a/dtc-mysql-plugin/test/mock-mysql.test.ts +++ b/dtc-mysql-plugin/test/mock-mysql.test.ts @@ -11,16 +11,16 @@ import invalidQueryTestCase from './fixtures/mock-invalid-query' const __dirname = dirname(fileURLToPath(import.meta.url)) test('It mocks mysql calls', async () => { - await executeTestCase(mockTestCase.narrow, [new FunctionCallPlugin(`${__dirname}/fixtures`), new MysqlMockPlugin()]) + await executeTestCase(mockTestCase.narrow, [new FunctionCallPlugin(), new MysqlMockPlugin()], `${__dirname}/fixtures/mock.ts`) }) test('It supports more than one mysql connection', async () => { - await executeTestCase(mockTwoConnectionsTestCase.narrow, [new FunctionCallPlugin(`${__dirname}/fixtures`), new MysqlMockPlugin()]) + await executeTestCase(mockTwoConnectionsTestCase.narrow, [new FunctionCallPlugin(), new MysqlMockPlugin()], `${__dirname}/fixtures/mockTwoConnectionsTestCase.ts`) }) test('It validates SQL queries', async () => { try { - await executeTestCase(invalidQueryTestCase.narrow, [new FunctionCallPlugin(`${__dirname}/fixtures`), new MysqlMockPlugin()]) + await executeTestCase(invalidQueryTestCase.narrow, [new FunctionCallPlugin(), new MysqlMockPlugin()], `${__dirname}/fixtures/invalidQueryTestCase.ts`) } catch (e) { if (e.code === 'ERR_ASSERTION' && e.operator === 'doesNotThrow') { assert.ok(true) diff --git a/dtc-playwright-plugin/package.json b/dtc-playwright-plugin/package.json index f1ef7f1..f839f56 100644 --- a/dtc-playwright-plugin/package.json +++ b/dtc-playwright-plugin/package.json @@ -1,6 +1,6 @@ { "name": "@cgauge/dtc-playwright-plugin", - "version": "0.2.2", + "version": "0.3.0", "description": "Playwright plugin for Declarative TestCases", "repository": { "type": "git", @@ -15,13 +15,14 @@ "author": "Abdala Cerqueira", "license": "LGPL-3.0-or-later", "dependencies": { - "@cgauge/assert": "^0.2.0", - "@cgauge/dtc": "^0.2.0", + "@cgauge/assert": "^0.3.0", + "@cgauge/dtc": "^0.3.0", "@playwright/test": "^1.47.0" }, "scripts": { "build": "rm -rf dist && tsc", "test": "tsx --test test/*.test.ts", + "test:only": "tsx --test --test-only test/*.test.ts", "test:coverage": "tsx --experimental-test-coverage --test test/*.test.ts" } } diff --git a/dtc-playwright-plugin/src/PlaywrightRunner.ts b/dtc-playwright-plugin/src/PlaywrightRunner.ts index 2220812..7d94b00 100644 --- a/dtc-playwright-plugin/src/PlaywrightRunner.ts +++ b/dtc-playwright-plugin/src/PlaywrightRunner.ts @@ -1,5 +1,6 @@ -import {Runner, TestCase, Plugins} from '@cgauge/dtc' +import {Runner, TestCaseExecution, Plugins} from '@cgauge/dtc' import {spawnSync} from 'node:child_process' +import nodeAssert from 'node:assert' import * as url from 'url' const __dirname = url.fileURLToPath(new URL('.', import.meta.url)) @@ -7,17 +8,19 @@ const __dirname = url.fileURLToPath(new URL('.', import.meta.url)) export class PlaywrightRunner implements Runner { constructor(private args: string[] = []) {} - async run(path: string, _testCase: TestCase, _plugins: Plugins, type: string, config?: string) { - spawnSync(`npx playwright test ${this.args.join(' ')} ${__dirname}`, { + async run(testCaseExecutions: TestCaseExecution[], _plugins: Plugins, type: string, config?: string) { + const childProcess = spawnSync(`npx playwright test ${this.args.join(' ')} ${__dirname}`, { stdio: 'inherit', shell: true, cwd: process.cwd(), env: { ...process.env, - DTC_PLAYWRIGHT_FILE_PATH: path, + ...(testCaseExecutions.length === 1 && {DTC_PLAYWRIGHT_PATH: testCaseExecutions[0].filePath}), DTC_PLAYWRIGHT_TYPE: type, DTC_PLAYWRIGHT_CONFIG: config, }, }) + + nodeAssert.equal(childProcess.status, 0) } } diff --git a/dtc-playwright-plugin/src/browser.spec.ts b/dtc-playwright-plugin/src/browser.spec.ts index 7e21e48..3f352ef 100644 --- a/dtc-playwright-plugin/src/browser.spec.ts +++ b/dtc-playwright-plugin/src/browser.spec.ts @@ -1,12 +1,12 @@ import {executeTestCase, resolveConfig} from '@cgauge/dtc' import {test} from '@playwright/test' -const path = String(process.env.DTC_PLAYWRIGHT_FILE_PATH) +const path = process.env.DTC_PLAYWRIGHT_PATH const type = String(process.env.DTC_PLAYWRIGHT_TYPE) const config = String(process.env.DTC_PLAYWRIGHT_CONFIG) -console.log(path, type, config) +const {testCaseExecutions, plugins} = await resolveConfig(path, config) -const {testCase, plugins} = await resolveConfig(path, config) - -test(testCase.name, async ({page}) => executeTestCase(testCase[type], plugins[type], {page})) +for (const {filePath, testCase} of testCaseExecutions) { + test(testCase.name, async ({page}) => executeTestCase(testCase[type], plugins[type], filePath, {page})) +} diff --git a/dtc-playwright-plugin/test/PlaywrightRunner.test.ts b/dtc-playwright-plugin/test/PlaywrightRunner.test.ts index dab621d..5683c8d 100644 --- a/dtc-playwright-plugin/test/PlaywrightRunner.test.ts +++ b/dtc-playwright-plugin/test/PlaywrightRunner.test.ts @@ -4,7 +4,8 @@ import assert from 'node:assert' import {fileURLToPath} from 'url' import {dirname} from 'path' import {PlaywrightPlugin} from '../src/PlaywrightPlugin' -import testCase from './fixtures/base.js' +import testCase from './fixtures/t1.dtc.js' +import testCase2 from './fixtures/t2.dtc.js' const __dirname = dirname(fileURLToPath(import.meta.url)) @@ -12,11 +13,13 @@ test('It calls playwright runner', async () => { const playwrightRunner = new PlaywrightRunner() await playwrightRunner.run( - `${__dirname}/fixtures/base.js`, - testCase, + [ + {filePath: `${__dirname}/fixtures/t1.dtc.js`, testCase}, + {filePath: `${__dirname}/fixtures/t2.dtc.js`, testCase: testCase2}, + ], {playwright: [new PlaywrightPlugin()]}, 'playwright', - `./dtc-playwright-plugin/test/fixtures/config.js`, + `./test/fixtures/config.js`, ) assert.ok(true) diff --git a/dtc-playwright-plugin/test/fixtures/config.js b/dtc-playwright-plugin/test/fixtures/config.js index 9f82f9f..f89346a 100644 --- a/dtc-playwright-plugin/test/fixtures/config.js +++ b/dtc-playwright-plugin/test/fixtures/config.js @@ -4,7 +4,9 @@ import {PlaywrightPlugin} from '../../src/PlaywrightPlugin.js' export default { runner: new PlaywrightRunner(), - plugins: (_basePath, _testCase) => ({ + testRegex: /^(?!.*node_modules).*dtc-playwright-plugin\/.*\.dtc\.[jt]s?$/, + + plugins: { playwright: [new PlaywrightPlugin()], - }) + }, } diff --git a/dtc-playwright-plugin/test/fixtures/base.js b/dtc-playwright-plugin/test/fixtures/t1.dtc.js similarity index 100% rename from dtc-playwright-plugin/test/fixtures/base.js rename to dtc-playwright-plugin/test/fixtures/t1.dtc.js diff --git a/dtc-playwright-plugin/test/fixtures/t2.dtc.js b/dtc-playwright-plugin/test/fixtures/t2.dtc.js new file mode 100644 index 0000000..8d21859 --- /dev/null +++ b/dtc-playwright-plugin/test/fixtures/t2.dtc.js @@ -0,0 +1,58 @@ +import {fileURLToPath} from 'url' +import {dirname} from 'path' + +const __dirname = dirname(fileURLToPath(import.meta.url)) + +export default { + name: 'Test case 2', + playwright: { + arrange: { + url: `file://${__dirname}/login.html`, + actions: [ + { + target: 'Username', + fill: '', + }, + { + target: { + name: 'getByPlaceholder', + args: ['Your password'], + }, + action: { + name: 'fill', + args: [''], + }, + }, + { + target: { + name: 'getByRole', + args: [ + 'button', + { + type: 'submit', + }, + ], + }, + action: { + name: 'click', + }, + }, + { + target: "input#username[required]:invalid", + toBeVisible: true, + }, + ], + }, + act: { + url: `file://${__dirname}/index.html`, + }, + assert: { + playwright: [ + { + target: "Index page", + toBeVisible: true, + }, + ], + }, + }, +} diff --git a/dtc/package.json b/dtc/package.json index 17f0060..1f761c3 100644 --- a/dtc/package.json +++ b/dtc/package.json @@ -1,6 +1,6 @@ { "name": "@cgauge/dtc", - "version": "0.2.2", + "version": "0.3.0", "description": "Declarative TestCases", "repository": { "type": "git", @@ -18,13 +18,15 @@ "author": "Abdala Cerqueira", "license": "LGPL-3.0-or-later", "dependencies": { - "@cgauge/assert": "^0.2.0", + "@cgauge/assert": "^0.3.0", "cleye": "^1.3.2", "nock": "^14.0.0-beta.11" }, "scripts": { "build": "rm -rf dist && tsc && chmod +x ./dist/cli.js", "test": "tsx --test test/*.test.ts", + "test:only": "tsx --test --test-only test/*.test.ts", + "test:single": "tsx --test", "test:coverage": "tsx --experimental-test-coverage --test test/*.test.ts" } } diff --git a/dtc/src/cli.ts b/dtc/src/cli.ts index afaa571..8e97c39 100755 --- a/dtc/src/cli.ts +++ b/dtc/src/cli.ts @@ -5,7 +5,7 @@ import {resolveConfig} from './config.js' const argv = cli({ name: 'cli.ts', - parameters: [''], + parameters: [], flags: { type: { alias: 't', @@ -23,12 +23,14 @@ const argv = cli({ const type = argv.flags.type const config = argv.flags.config -const path = `${process.cwd()}/${argv._.path}` +const filePath = argv._[0] ?? null -const {testCase, plugins, runner} = await resolveConfig(path, config) +const {testCaseExecutions, plugins, runner} = await resolveConfig(filePath, config) -if (!(type in testCase)) { - throw new Error(`Invalid test type (${type})`) +for (const {testCase} of testCaseExecutions) { + if (!(type in testCase)) { + throw new Error(`Invalid test type (${type})`) + } } if (!(type in plugins)) { @@ -39,4 +41,4 @@ if (!runner) { throw new Error(`No test runner found`) } -await runner.run(path, testCase, plugins, type, config) +await runner.run(testCaseExecutions, plugins, type, config) diff --git a/dtc/src/config.ts b/dtc/src/config.ts index e29be73..f1a80cc 100644 --- a/dtc/src/config.ts +++ b/dtc/src/config.ts @@ -1,27 +1,80 @@ -import {dirname} from 'node:path' -import type {Runner, Plugins, TestCase} from './domain' -import {defaultPlugins, defaultTestRunner} from './index.js' +import type {Runner, Plugins, TestCase, TestCaseExecution} from './domain' +import {defaultLoader, defaultPlugins, defaultTestRunner} from './index.js' +import {readdirSync, statSync} from 'fs' +import {join} from 'path' export type Config = { - plugins: (basePath: string, testCase: TestCase) => Plugins - load: (filePath: string) => TestCase + plugins: Plugins + loader: (filePath: string) => Promise runner: Runner + testDir: string + testRegex: string } -export const resolveConfig = async (path: string, config?: string) => { - let testCase: TestCase - let plugins: Plugins +export const resolveConfig = async (filePath?: string, configPath?: string) => { + const projectPath = process.cwd() let runner: Runner = defaultTestRunner + let plugins = defaultPlugins + let loader = defaultLoader + let testRegex = /.*\.dtc\.[jt]s?$/ + + if (configPath) { + const { + plugins: customPlugins, + loader: customLoad, + runner: customTestRunner, + testRegex: customTestRegex, + } = await defaultLoader(`${process.cwd()}/${configPath}`) - if (config) { - const {plugins: customPlugins, load: customLoad, runner: customTestRunner} = (await import(`${process.cwd()}/${config}`)).default - testCase = customLoad ? await customLoad(path) : (await import(path)).default - plugins = customPlugins ? customPlugins(dirname(path), testCase) : defaultPlugins(dirname(path)) runner = customTestRunner ?? defaultTestRunner - } else { - testCase = (await import(path)).default - plugins = defaultPlugins(dirname(path)) + loader = customLoad ?? loader + plugins = customPlugins ? customPlugins : defaultPlugins + testRegex = customTestRegex ?? testRegex } - return {testCase, plugins, runner} + const testCaseExecutions = await getTestCases(projectPath, loader, testRegex, filePath) + + return {testCaseExecutions, plugins, runner} } + +const generateTestCaseExecution = async (filePath: string, loader: Config['loader']): Promise => ({ + filePath, + testCase: await loader(filePath), +}) + +const getTestCases = async ( + projectPath: string, + loader: Config['loader'] | null, + testRegex: RegExp, + filePath?: string, +): Promise => { + if (filePath) { + return loader + ? [await generateTestCaseExecution(filePath, loader)] + : [await generateTestCaseExecution(filePath, defaultLoader)] + } + + const files = loadTestFiles(projectPath, testRegex) + + return await Promise.all( + files.map((file) => + loader ? generateTestCaseExecution(file, loader) : generateTestCaseExecution(file, defaultLoader), + ), + ) +} + +export const loadTestFiles = (currentPath: string, testRegex: RegExp): string[] => + readdirSync(currentPath) + .map((file) => { + const filePath = join(currentPath, file) + + if (statSync(filePath).isDirectory()) { + return loadTestFiles(filePath, testRegex) + } else if (testRegex.test(filePath)) { + return filePath + } + + return null + }) + .filter((item) => item !== null) + .flat() diff --git a/dtc/src/domain.ts b/dtc/src/domain.ts index b2f3caa..29dc2c9 100644 --- a/dtc/src/domain.ts +++ b/dtc/src/domain.ts @@ -1,33 +1,39 @@ export interface Plugin { - setTestRunnerArgs?: (args: unknown) => void, - arrange?: (args: unknown) => void, - act?: (args: unknown) => void, - assert?: (args: unknown) => void, - clean?: (args: unknown) => void, + setTestRunnerArgs?: (args: unknown) => void + setBasePath?: (path: string) => void + arrange?: (args: unknown) => void + act?: (args: unknown) => void + assert?: (args: unknown) => void + clean?: (args: unknown) => void } export type Plugins = { - [type: string]: Plugin[], + [type: string]: Plugin[] } export interface Runner { - run: (path: string, testCase: TestCase, plugins: Plugins, type: string, config?: string) => Promise, + run: (testCases: TestCaseExecution[], plugins: Plugins, type: string, config?: string) => Promise } export type TypeTestCase = { retry?: number delay?: number - arrange?: Record, - act?: Record, - assert?: unknown, - clean?: Record, + arrange?: Record + act?: Record + assert?: unknown + clean?: Record } type CommonTestCase = { - name: string, - debug?: boolean, -} + name: string + debug?: boolean +} export type TestCase = CommonTestCase & { - [type: string]: TypeTestCase, + [type: string]: TypeTestCase +} + +export type TestCaseExecution = { + filePath: string + testCase: TestCase } diff --git a/dtc/src/index.ts b/dtc/src/index.ts index 38a6d2a..bddf06e 100644 --- a/dtc/src/index.ts +++ b/dtc/src/index.ts @@ -5,6 +5,7 @@ import {HttpCallPlugin} from './plugins/HttpCallPlugin.js' import {HttpMockPlugin} from './plugins/HttpMockPlugin.js' import {NodeTestRunner} from './runners/NodeTestRunner.js' import {retry} from './utils.js' +import {dirname} from 'node:path' export type * from './domain' export * from './utils.js' @@ -14,19 +15,29 @@ export {DisableNetConnectPlugin, FunctionCallPlugin, HttpCallPlugin, HttpMockPlu export const defaultTestRunner = new NodeTestRunner() -export const defaultPlugins = (basePath: string): Plugins => ({ - unit: [new DisableNetConnectPlugin(), new FunctionCallPlugin(basePath)], - narrow: [new DisableNetConnectPlugin(), new FunctionCallPlugin(basePath), new HttpMockPlugin()], - broad: [new FunctionCallPlugin(basePath), new HttpCallPlugin()], -}) +export const defaultLoader = async (filePath: string) => (await import(filePath)).default -export const executeTestCase = async (testCase: TypeTestCase, plugins: Plugin[], testRunnerArgs?: unknown) => { - plugins.forEach((plugin) => plugin.setTestRunnerArgs?.(testRunnerArgs)) +export const defaultPlugins: Plugins = { + unit: [new DisableNetConnectPlugin(), new FunctionCallPlugin()], + narrow: [new DisableNetConnectPlugin(), new FunctionCallPlugin(), new HttpMockPlugin()], + broad: [new FunctionCallPlugin(), new HttpCallPlugin()], +} + +export const executeTestCase = async ( + testCase: TypeTestCase, + plugins: Plugin[], + filePath: string, + testRunnerArgs?: unknown, +) => { + plugins.forEach((plugin) => { + plugin.setTestRunnerArgs?.(testRunnerArgs) + plugin.setBasePath?.(dirname(filePath)) + }) await Promise.all(plugins.map((plugin) => plugin.arrange?.(testCase.arrange))) await Promise.all(plugins.map((plugin) => plugin.act?.(testCase.act))) await Promise.all( - plugins.map((plugin) => + plugins.map((plugin) => retry(async () => plugin.assert?.(testCase.assert), testCase.retry ?? 0, testCase.delay ?? 0), ), ) diff --git a/dtc/src/plugins/FunctionCallPlugin.ts b/dtc/src/plugins/FunctionCallPlugin.ts index 3ad0c07..8e682bc 100644 --- a/dtc/src/plugins/FunctionCallPlugin.ts +++ b/dtc/src/plugins/FunctionCallPlugin.ts @@ -12,8 +12,11 @@ const isFunctionCallAct = (v: unknown): v is FunctionCallAct => typeof v === 'ob export class FunctionCallPlugin implements Plugin { private response: any + private basePath?: string - constructor(private readonly basePath: string) {} + setBasePath(path: string) { + this.basePath = path + } async act(args: unknown): Promise { if (!isFunctionCallAct(args)) { diff --git a/dtc/src/runners/NodeTestRunner.ts b/dtc/src/runners/NodeTestRunner.ts index e41c460..fb7974a 100644 --- a/dtc/src/runners/NodeTestRunner.ts +++ b/dtc/src/runners/NodeTestRunner.ts @@ -1,9 +1,11 @@ import {executeTestCase} from '../index.js' -import type {Runner, TestCase, Plugins} from '../domain.js' +import type {Runner, TestCaseExecution, Plugins} from '../domain.js' import test from 'node:test' export class NodeTestRunner implements Runner { - async run(_path: string, testCase: TestCase, plugins: Plugins, type: string): Promise { - test(testCase.name, (args) => executeTestCase(testCase[type], plugins[type], args)) + async run(testCaseExecutions: TestCaseExecution[], plugins: Plugins, type: string): Promise { + for (const {filePath, testCase} of testCaseExecutions) { + test(testCase.name, (args) => executeTestCase(testCase[type], plugins[type], filePath, args)) + } } } diff --git a/dtc/test/cli.test.ts b/dtc/test/cli.test.ts index 87251a3..3e6e29e 100644 --- a/dtc/test/cli.test.ts +++ b/dtc/test/cli.test.ts @@ -1,9 +1,23 @@ import {test} from 'node:test' import {spawnSync} from 'node:child_process' import nodeAssert from 'node:assert' +import { dirname } from 'node:path' +import {fileURLToPath} from 'node:url' + +const __dirname = dirname(fileURLToPath(import.meta.url)) test('It executes based unit test using cli', async () => { - const childProcess = spawnSync(`npx tsx src/cli.ts test/fixtures/unit.js`, { + const childProcess = spawnSync(`npx tsx ${__dirname}/../src/cli.ts ${__dirname}/fixtures/unit.js`, { + stdio: 'inherit', + shell: true, + cwd: process.cwd(), + }) + + nodeAssert.equal(childProcess.status, 0) +}) + +test('It executes all files', async () => { + const childProcess = spawnSync(`npx tsx ${__dirname}/../src/cli.ts`, { stdio: 'inherit', shell: true, cwd: process.cwd(), diff --git a/dtc/test/fixtures/t1.dtc.ts b/dtc/test/fixtures/t1.dtc.ts new file mode 100644 index 0000000..973e841 --- /dev/null +++ b/dtc/test/fixtures/t1.dtc.ts @@ -0,0 +1,13 @@ +export default { + name: 'Test 1', + unit: { + act: { + import: 'syncFunction', + from: 'functions.js', + attributes: [{a: 'b'}], + }, + assert: { + a: 'b', + }, + }, +} diff --git a/dtc/test/fixtures/t2.dtc.ts b/dtc/test/fixtures/t2.dtc.ts new file mode 100644 index 0000000..783dd54 --- /dev/null +++ b/dtc/test/fixtures/t2.dtc.ts @@ -0,0 +1,13 @@ +export default { + name: 'Test 2', + unit: { + act: { + import: 'syncFunction', + from: 'functions.js', + attributes: [{a: 'b'}], + }, + assert: { + a: 'b', + }, + }, +} diff --git a/dtc/test/fixtures/tests/t3.dtc.ts b/dtc/test/fixtures/tests/t3.dtc.ts new file mode 100644 index 0000000..9d1afcb --- /dev/null +++ b/dtc/test/fixtures/tests/t3.dtc.ts @@ -0,0 +1,13 @@ +export default { + name: 'Test 3', + unit: { + act: { + import: 'syncFunction', + from: '../functions.js', + attributes: [{a: 'b'}], + }, + assert: { + a: 'b', + }, + }, +} diff --git a/dtc/test/fixtures/unit.js b/dtc/test/fixtures/unit.js index b3b6a1b..6413abb 100644 --- a/dtc/test/fixtures/unit.js +++ b/dtc/test/fixtures/unit.js @@ -1,8 +1,9 @@ export default { + name: 'Unit test', unit: { act: { import: 'syncFunction', - from: 'functions.js', + from: './functions.js', attributes: [ {a: 'b'} ] diff --git a/dtc/test/index.test.ts b/dtc/test/index.test.ts index fb8627d..ea8fb53 100644 --- a/dtc/test/index.test.ts +++ b/dtc/test/index.test.ts @@ -22,12 +22,13 @@ test('It runs plugins methods', async () => { test('It pass parameters to plugins methods', async () => { const setTestRunnerArgs = mock.fn() + const setBasePath = mock.fn() const arrange = mock.fn() const act = mock.fn() const assert = mock.fn() const clean = mock.fn() - const mockedPlugin = {setTestRunnerArgs, arrange, act, assert, clean} + const mockedPlugin = {setTestRunnerArgs, setBasePath, arrange, act, assert, clean} const testCase = { arrange: {a: 'b'}, @@ -35,9 +36,10 @@ test('It pass parameters to plugins methods', async () => { assert: {e: 'f'}, clean: {g: 'h'}, } + const filePath = 'path/to/file' const testArgs = {i: 'j'} - await executeTestCase(testCase, [mockedPlugin], testArgs) + await executeTestCase(testCase, [mockedPlugin], filePath, testArgs) nodeAssert.deepEqual(setTestRunnerArgs.mock.calls[0].arguments, [testArgs]) nodeAssert.deepEqual(arrange.mock.calls[0].arguments, [testCase.arrange]) @@ -47,7 +49,7 @@ test('It pass parameters to plugins methods', async () => { }) test('It ensure plugins methods are optional', async () => { - await executeTestCase({}, [{}], {}) + await executeTestCase({}, [{}], '', {}) nodeAssert.ok(true) }) diff --git a/nock-aws/package.json b/nock-aws/package.json index 74b114b..d451db7 100644 --- a/nock-aws/package.json +++ b/nock-aws/package.json @@ -1,6 +1,6 @@ { "name": "@cgauge/nock-aws", - "version": "0.2.2", + "version": "0.3.0", "description": "AWS Request mocker based on Nock", "repository": { "type": "git", @@ -28,6 +28,7 @@ "scripts": { "build": "rm -rf dist && tsc", "test": "AWS_MAX_ATTEMPTS=0 AWS_REGION=eu-west-1 AWS_ACCESS_KEY_ID=test AWS_SECRET_ACCESS_KEY=test tsx --test test/*.test.ts", + "test:only": "AWS_MAX_ATTEMPTS=0 AWS_REGION=eu-west-1 AWS_ACCESS_KEY_ID=test AWS_SECRET_ACCESS_KEY=test tsx --test --test-only test/*.test.ts", "test:coverage": "AWS_MAX_ATTEMPTS=0 AWS_REGION=eu-west-1 AWS_ACCESS_KEY_ID=test AWS_SECRET_ACCESS_KEY=test tsx --experimental-test-coverage --test test/*.test.ts" } } diff --git a/package-lock.json b/package-lock.json index 947893e..e12bca2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -24,7 +24,7 @@ }, "assert": { "name": "@cgauge/assert", - "version": "0.2.2", + "version": "0.3.0", "license": "LGPL-3.0-or-later", "dependencies": { "@jest/expect-utils": "^29.7.0" @@ -32,10 +32,10 @@ }, "dtc": { "name": "@cgauge/dtc", - "version": "0.2.2", + "version": "0.3.0", "license": "LGPL-3.0-or-later", "dependencies": { - "@cgauge/assert": "^0.2.0", + "@cgauge/assert": "^0.3.0", "cleye": "^1.3.2", "nock": "^14.0.0-beta.11" }, @@ -45,7 +45,7 @@ }, "dtc-aws-plugin": { "name": "@cgauge/dtc-aws-plugin", - "version": "0.2.2", + "version": "0.3.0", "license": "LGPL-3.0-or-later", "dependencies": { "@aws-sdk/client-dynamodb": "^3.645.0", @@ -54,35 +54,44 @@ "@aws-sdk/client-sns": "^3.645.0", "@aws-sdk/lib-dynamodb": "^3.645.0", "@aws-sdk/util-dynamodb": "^3.645.0", - "@cgauge/assert": "^0.2.0", - "@cgauge/dtc": "^0.2.0", - "@cgauge/nock-aws": "^0.2.0" + "@cgauge/assert": "^0.3.0", + "@cgauge/dtc": "^0.3.0", + "@cgauge/nock-aws": "^0.3.0" } }, "dtc-mysql-plugin": { "name": "@cgauge/dtc-mysql-plugin", - "version": "0.2.2", + "version": "0.3.0", "license": "LGPL-3.0-or-later", "dependencies": { - "@cgauge/assert": "^0.2.0", - "@cgauge/dtc": "^0.2.0", + "@cgauge/assert": "^0.3.0", + "@cgauge/dtc": "^0.3.0", "mysql2": "^3.11.0", "node-sql-parser": "^5.1.0" } }, "dtc-playwright-plugin": { "name": "@cgauge/dtc-playwright-plugin", - "version": "0.2.2", + "version": "0.3.0", "license": "LGPL-3.0-or-later", "dependencies": { - "@cgauge/assert": "^0.2.0", - "@cgauge/dtc": "^0.2.0", + "@cgauge/assert": "^0.3.0", + "@cgauge/dtc": "^0.3.0", "@playwright/test": "^1.47.0" } }, + "dtc/node_modules/@cgauge/assert": { + "version": "0.2.2", + "resolved": "https://registry.npmjs.org/@cgauge/assert/-/assert-0.2.2.tgz", + "integrity": "sha512-iOXUAtC0nCaN9eIjKkK6saBbeBLsLkQmjbz/ydDzGG3Dn4/QPo96fIYWMDVspX6q+FkS8gHCiiatchqdo0zPSQ==", + "license": "LGPL-3.0-or-later", + "dependencies": { + "@jest/expect-utils": "^29.7.0" + } + }, "nock-aws": { "name": "@cgauge/nock-aws", - "version": "0.2.2", + "version": "0.3.0", "license": "LGPL-3.0-or-later", "dependencies": { "nock": "^14.0.0-beta.11" @@ -2407,7 +2416,7 @@ }, "yaml": { "name": "@cgauge/yaml", - "version": "0.2.2", + "version": "0.3.0", "license": "LGPL-3.0-or-later", "dependencies": { "js-yaml": "^4.1.0" diff --git a/package.json b/package.json index 783c7c6..9813de5 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,8 @@ ], "scripts": { "build": "npm run build --workspaces --if-present", - "test": "npm run test --workspaces --if-present" + "test": "npm run test --workspaces --if-present", + "test:only": "npm run test:only --workspaces --if-present" }, "devDependencies": { "@types/node": "^20.0.0", diff --git a/yaml/package.json b/yaml/package.json index ba61b78..a5c16ec 100644 --- a/yaml/package.json +++ b/yaml/package.json @@ -1,6 +1,6 @@ { "name": "@cgauge/yaml", - "version": "0.2.2", + "version": "0.3.0", "description": "YAML parser with extra tags", "repository": { "type": "git", @@ -24,6 +24,7 @@ "scripts": { "build": "rm -rf dist && tsc", "test": "tsx --env-file .env --test test/*.test.ts", + "test:only": "tsx --env-file .env --test --test-only test/*.test.ts", "test:coverage": "tsx --experimental-test-coverage --test test/*.test.ts" } }