Skip to content

Commit

Permalink
add options to run all dtc test files in a project (#6)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
fantazista and abdala authored Oct 4, 2024
1 parent a581baf commit d746ecb
Show file tree
Hide file tree
Showing 29 changed files with 323 additions and 103 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion assert/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cgauge/assert",
"version": "0.2.2",
"version": "0.3.0",
"description": "Extra assert library",
"type": "module",
"repository": {
Expand All @@ -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"
}
}
9 changes: 5 additions & 4 deletions dtc-aws-plugin/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
Expand All @@ -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"
}
}
7 changes: 4 additions & 3 deletions dtc-mysql-plugin/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
}
}
6 changes: 3 additions & 3 deletions dtc-mysql-plugin/test/mock-mysql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions dtc-playwright-plugin/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"
}
}
11 changes: 7 additions & 4 deletions dtc-playwright-plugin/src/PlaywrightRunner.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
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))

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)
}
}
10 changes: 5 additions & 5 deletions dtc-playwright-plugin/src/browser.spec.ts
Original file line number Diff line number Diff line change
@@ -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}))
}
11 changes: 7 additions & 4 deletions dtc-playwright-plugin/test/PlaywrightRunner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@ 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))

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)
Expand Down
6 changes: 4 additions & 2 deletions dtc-playwright-plugin/test/fixtures/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()],
})
},
}
File renamed without changes.
58 changes: 58 additions & 0 deletions dtc-playwright-plugin/test/fixtures/t2.dtc.js
Original file line number Diff line number Diff line change
@@ -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,
},
],
},
},
}
6 changes: 4 additions & 2 deletions dtc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cgauge/dtc",
"version": "0.2.2",
"version": "0.3.0",
"description": "Declarative TestCases",
"repository": {
"type": "git",
Expand All @@ -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"
}
}
14 changes: 8 additions & 6 deletions dtc/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {resolveConfig} from './config.js'

const argv = cli({
name: 'cli.ts',
parameters: ['<path>'],
parameters: [],
flags: {
type: {
alias: 't',
Expand All @@ -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)) {
Expand All @@ -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)
Loading

0 comments on commit d746ecb

Please sign in to comment.