Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion dtc-playwright-plugin/test/fixtures/config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import playwrightRunner from '../../src/playwright-runner.js'
import * as playwrightPlugin from '../../src/playwright-plugin.js'

export default {
runner: playwrightRunner(),
testRegex: /^(?!.*node_modules).*dtc-playwright-plugin\/.*\.dtc\.[jt]s?$/,
plugins: ['../../dtc-playwright-plugin/src/playwright-plugin.js']
plugins: [playwrightPlugin]
}
16 changes: 8 additions & 8 deletions dtc-playwright-plugin/test/playwright-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,36 @@ import nodeAssert from 'node:assert'

const page = {goto: mock.fn()}

test('It does not arrange if type does not match', () => arrange(
test('It does not arrange if type does not match', async () => { await arrange(
{},
'basePath',
//@ts-ignore
{page}
))
)})

test('It does not act if type does not match', () => act(
test('It does not act if type does not match', async () => { await act(
{},
'basePath',
//@ts-ignore
{page}
))
)})

test('It does not act if type does not match 2', () => act(
test('It does not act if type does not match 2', async () => { await act(
{
url: 'https://customergauge.com',
script: './script.js',
},
'basePath',
//@ts-ignore
{page}
))
)})

test('It does not assert if type does not match', () => assert(
test('It does not assert if type does not match', async () => { await assert(
{},
'basePath',
//@ts-ignore
{page}
))
)})

test('It calls playwright triggers', async () => {
const page = {goto: mock.fn()}
Expand Down
4 changes: 2 additions & 2 deletions dtc/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type {Runner, Loader} from './domain'
import type {Runner, Loader, Plugin} from './domain'
import {defaultLoader, defaultPlugins, defaultTestRunner} from './index.js'

export type Config = {
plugins: string[]
plugins: Plugin[]
loader: Loader
runner: Runner
testRegex: RegExp
Expand Down
13 changes: 12 additions & 1 deletion dtc/src/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,22 @@ export type Loader = <T = TestCase>(filePath: string) => Promise<T>

export type Runner = (
testCases: TestCaseExecution[],
plugins: string[],
plugins: Plugin[],
args?: string[],
config?: string,
) => Promise<void>

type RequireAtLeastOne<T> = {
[K in keyof T]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<keyof T, K>>>
}[keyof T]

export type Plugin = RequireAtLeastOne<{
arrange(args: unknown, basePath: string, testRunnerArgs?: unknown): Promise<boolean>
act(args: unknown, basePath: string, testRunnerArgs?: unknown): Promise<boolean>
assert(args: unknown, basePath: string, testRunnerArgs?: unknown): Promise<boolean>
clean(args: unknown, basePath: string, testRunnerArgs?: unknown): Promise<boolean>
}>

export type TestCasePhases = 'arrange' | 'act' | 'assert' | 'clean'

const GenericAttributes = record(
Expand Down
26 changes: 12 additions & 14 deletions dtc/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
import type {TestCase, TestCaseExecution, TestCasePhases} from './domain'
import type {Plugin, TestCase, TestCaseExecution, TestCasePhases} from './domain'
import {debug, retry} from './utils.js'
import {dirname} from 'node:path'
import test from 'node:test'
import * as disableNetConnectPlugin from './plugins/disable-net-connect-plugin.js'
import * as functionCallPlugin from './plugins/function-call-plugin.js'
import * as httpMockPlugin from './plugins/http-mock-plugin.js'

export type * from './domain'
export * from './utils.js'
export * from './config.js'
export * from './loader.js'

export const defaultTestRunner = async (testCaseExecutions: TestCaseExecution[], plugins: string[]) => {
export const defaultTestRunner = async (testCaseExecutions: TestCaseExecution[], plugins: Plugin[]) => {
for (const testCaseExecution of testCaseExecutions) {
test(testCaseExecution.testCase.name ?? testCaseExecution.filePath, (args) => executeTestCase(testCaseExecution, plugins, args))
}
}

export const defaultLoader = async (filePath: string) => (await import(filePath)).default

export const defaultPlugins = [
'./plugins/disable-net-connect-plugin.js',
'./plugins/function-call-plugin.js',
'./plugins/http-mock-plugin.js',
]
export const defaultPlugins: Plugin[] = [disableNetConnectPlugin, functionCallPlugin, httpMockPlugin]

const createPluginExecutor = (plugins: any[], basePath: string, testRunnerArgs?: unknown) => {
return async (functionName: string, data: unknown) => {
const createPluginExecutor = (plugins: Plugin[], basePath: string, testRunnerArgs?: unknown) => {
return async (functionName: TestCasePhases, data: unknown) => {
if (!data) {
return
}
Expand All @@ -44,7 +43,7 @@ const createPluginExecutor = (plugins: any[], basePath: string, testRunnerArgs?:
}

const createPhaseExecutor = (
executePluginFunction: (functionName: string, data: unknown) => Promise<void>,
executePluginFunction: (functionName: TestCasePhases, data: unknown) => Promise<void>,
testCaseExecution: TestCaseExecution,
) => {
const allowedLayerPhases = ['arrange', 'clean']
Expand Down Expand Up @@ -82,12 +81,11 @@ const createPhaseExecutor = (

const createTestCaseExecutor = async (
testCaseExecution: TestCaseExecution,
plugins: string[],
plugins: Plugin[],
testRunnerArgs?: unknown,
) => {
const basePath = dirname(testCaseExecution.filePath)
const loadedPlugins = await Promise.all(plugins.map((plugin) => import(plugin)))
const executePluginFunction = createPluginExecutor(loadedPlugins, basePath, testRunnerArgs)
const executePluginFunction = createPluginExecutor(plugins, basePath, testRunnerArgs)
const phaseExecutor = createPhaseExecutor(executePluginFunction, testCaseExecution)
let errors: Error[] = []

Expand Down Expand Up @@ -118,7 +116,7 @@ const createTestCaseExecutor = async (

export const executeTestCase = async (
testCaseExecution: TestCaseExecution,
plugins: string[],
plugins: Plugin[],
testRunnerArgs?: unknown,
) => {
debug(`TestCase: ${JSON.stringify(testCaseExecution, null, 2)}`)
Expand Down
2 changes: 1 addition & 1 deletion dtc/src/plugins/disable-net-connect-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import nock from 'nock'

export const arrange = (): boolean => {
export const arrange = async (): Promise<boolean> => {
nock.disableNetConnect()
return true
}
2 changes: 1 addition & 1 deletion dtc/src/plugins/function-call-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const act = async (args: unknown, basePath: string): Promise<boolean> =>
return true
}

export const assert = (args: unknown): boolean => {
export const assert = async (args: unknown): Promise<boolean> => {
if (!is(args, FunctionCallResponse)) {
return false
}
Expand Down
2 changes: 1 addition & 1 deletion dtc/src/plugins/http-mock-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const arrange = async (args: unknown): Promise<boolean> => {
return true
}

export const assert = (): boolean => {
export const assert = async (): Promise<boolean> => {
if (!nock.isDone()) {
const pendingUrls = nock.pendingMocks()
nock.cleanAll()
Expand Down
6 changes: 0 additions & 6 deletions dtc/test/fixtures/layers.ts

This file was deleted.

8 changes: 4 additions & 4 deletions dtc/test/fixtures/plugin-act-false.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {mock} from 'node:test'

export const arrange = mock.fn()
export const act = mock.fn(() => false)
export const assert = mock.fn()
export const clean = mock.fn()
export const arrange = mock.fn(async () => true)
export const act = mock.fn(async () => false)
export const assert = mock.fn(async () => true)
export const clean = mock.fn(async () => true)
8 changes: 4 additions & 4 deletions dtc/test/fixtures/plugin-args.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {mock} from 'node:test'

export const arrange = mock.fn(() => true)
export const act = mock.fn(() => true)
export const assert = mock.fn(() => true)
export const clean = mock.fn(() => true)
export const arrange = mock.fn(async () => true)
export const act = mock.fn(async () => true)
export const assert = mock.fn(async () => true)
export const clean = mock.fn(async () => true)
8 changes: 4 additions & 4 deletions dtc/test/fixtures/plugin-array.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {mock} from 'node:test'

export const arrange = mock.fn(() => true)
export const act = mock.fn(() => true)
export const assert = mock.fn(() => true)
export const clean = mock.fn(() => true)
export const arrange = mock.fn(async () => true)
export const act = mock.fn(async () => true)
export const assert = mock.fn(async () => true)
export const clean = mock.fn(async () => true)
8 changes: 4 additions & 4 deletions dtc/test/fixtures/plugin-cleanup.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {mock} from 'node:test'

export const arrange = mock.fn(() => true)
export const act = mock.fn(() => {
export const arrange = mock.fn(async () => true)
export const act = mock.fn(async () => {
throw new Error('Act failed')
})
export const assert = mock.fn(() => true)
export const clean = mock.fn(() => true)
export const assert = mock.fn(async () => true)
export const clean = mock.fn(async () => true)
6 changes: 6 additions & 0 deletions dtc/test/fixtures/plugin-layers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import {mock} from 'node:test'

export const arrange = mock.fn(async () => true)
export const act = mock.fn(async () => true)
export const assert = mock.fn(async () => true)
export const clean = mock.fn(async () => true)
8 changes: 4 additions & 4 deletions dtc/test/fixtures/plugin-retry.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {mock} from 'node:test'

export const arrange = mock.fn(() => true)
export const act = mock.fn(() => true)
export const assert = mock.fn((data) => {
export const arrange = mock.fn(async () => true)
export const act = mock.fn(async () => true)
export const assert = mock.fn(async () => {
if (assert.mock.callCount() < 2) {
throw new Error('Failing assertion')
}
return true
})
export const clean = mock.fn(() => true)
export const clean = mock.fn(async () => true)
8 changes: 4 additions & 4 deletions dtc/test/fixtures/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {mock} from 'node:test'

export const arrange = mock.fn(() => true)
export const act = mock.fn(() => true)
export const assert = mock.fn(() => true)
export const clean = mock.fn(() => true)
export const arrange = mock.fn(async () => true)
export const act = mock.fn(async () => true)
export const assert = mock.fn(async () => true)
export const clean = mock.fn(async () => true)
Loading