Skip to content

Commit 2a9ebcd

Browse files
committed
Create explicit plugin contract
1 parent bf16c7f commit 2a9ebcd

21 files changed

+115
-131
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import playwrightRunner from '../../src/playwright-runner.js'
2+
import * as playwrightPlugin from '../../src/playwright-plugin.js'
23

34
export default {
45
runner: playwrightRunner(),
56
testRegex: /^(?!.*node_modules).*dtc-playwright-plugin\/.*\.dtc\.[jt]s?$/,
6-
plugins: ['../../dtc-playwright-plugin/src/playwright-plugin.js']
7+
plugins: [playwrightPlugin]
78
}

dtc-playwright-plugin/test/playwright-plugin.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,36 @@ import nodeAssert from 'node:assert'
44

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

7-
test('It does not arrange if type does not match', () => arrange(
7+
test('It does not arrange if type does not match', async () => { await arrange(
88
{},
99
'basePath',
1010
//@ts-ignore
1111
{page}
12-
))
12+
)})
1313

14-
test('It does not act if type does not match', () => act(
14+
test('It does not act if type does not match', async () => { await act(
1515
{},
1616
'basePath',
1717
//@ts-ignore
1818
{page}
19-
))
19+
)})
2020

21-
test('It does not act if type does not match 2', () => act(
21+
test('It does not act if type does not match 2', async () => { await act(
2222
{
2323
url: 'https://customergauge.com',
2424
script: './script.js',
2525
},
2626
'basePath',
2727
//@ts-ignore
2828
{page}
29-
))
29+
)})
3030

31-
test('It does not assert if type does not match', () => assert(
31+
test('It does not assert if type does not match', async () => { await assert(
3232
{},
3333
'basePath',
3434
//@ts-ignore
3535
{page}
36-
))
36+
)})
3737

3838
test('It calls playwright triggers', async () => {
3939
const page = {goto: mock.fn()}

dtc/src/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import type {Runner, Loader} from './domain'
1+
import type {Runner, Loader, Plugin} from './domain'
22
import {defaultLoader, defaultPlugins, defaultTestRunner} from './index.js'
33

44
export type Config = {
5-
plugins: string[]
5+
plugins: Plugin[]
66
loader: Loader
77
runner: Runner
88
testRegex: RegExp

dtc/src/domain.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@ export type Loader = <T = TestCase>(filePath: string) => Promise<T>
44

55
export type Runner = (
66
testCases: TestCaseExecution[],
7-
plugins: string[],
7+
plugins: Plugin[],
88
args?: string[],
99
config?: string,
1010
) => Promise<void>
1111

12+
export type Plugin = {
13+
arrange?(args: unknown, basePath: string, testRunnerArgs?: unknown): Promise<boolean>
14+
act?(args: unknown, basePath: string, testRunnerArgs?: unknown): Promise<boolean>
15+
assert?(args: unknown, basePath: string, testRunnerArgs?: unknown): Promise<boolean>
16+
clean?(args: unknown, basePath: string, testRunnerArgs?: unknown): Promise<boolean>
17+
}
18+
1219
export type TestCasePhases = 'arrange' | 'act' | 'assert' | 'clean'
1320

1421
const GenericAttributes = record(

dtc/src/index.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
1-
import type {TestCase, TestCaseExecution, TestCasePhases} from './domain'
1+
import type {Plugin, TestCase, TestCaseExecution, TestCasePhases} from './domain'
22
import {debug, retry} from './utils.js'
33
import {dirname} from 'node:path'
44
import test from 'node:test'
5+
import * as disableNetConnectPlugin from './plugins/disable-net-connect-plugin.js'
6+
import * as functionCallPlugin from './plugins/function-call-plugin.js'
7+
import * as httpMockPlugin from './plugins/http-mock-plugin.js'
58

69
export type * from './domain'
710
export * from './utils.js'
811
export * from './config.js'
912
export * from './loader.js'
1013

11-
export const defaultTestRunner = async (testCaseExecutions: TestCaseExecution[], plugins: string[]) => {
14+
export const defaultTestRunner = async (testCaseExecutions: TestCaseExecution[], plugins: Plugin[]) => {
1215
for (const testCaseExecution of testCaseExecutions) {
1316
test(testCaseExecution.testCase.name ?? testCaseExecution.filePath, (args) => executeTestCase(testCaseExecution, plugins, args))
1417
}
1518
}
1619

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

19-
export const defaultPlugins = [
20-
'./plugins/disable-net-connect-plugin.js',
21-
'./plugins/function-call-plugin.js',
22-
'./plugins/http-mock-plugin.js',
23-
]
22+
export const defaultPlugins: Plugin[] = [disableNetConnectPlugin, functionCallPlugin, httpMockPlugin]
2423

25-
const createPluginExecutor = (plugins: any[], basePath: string, testRunnerArgs?: unknown) => {
26-
return async (functionName: string, data: unknown) => {
24+
const createPluginExecutor = (plugins: Plugin[], basePath: string, testRunnerArgs?: unknown) => {
25+
return async (functionName: TestCasePhases, data: unknown) => {
2726
if (!data) {
2827
return
2928
}
@@ -44,7 +43,7 @@ const createPluginExecutor = (plugins: any[], basePath: string, testRunnerArgs?:
4443
}
4544

4645
const createPhaseExecutor = (
47-
executePluginFunction: (functionName: string, data: unknown) => Promise<void>,
46+
executePluginFunction: (functionName: TestCasePhases, data: unknown) => Promise<void>,
4847
testCaseExecution: TestCaseExecution,
4948
) => {
5049
const allowedLayerPhases = ['arrange', 'clean']
@@ -82,12 +81,11 @@ const createPhaseExecutor = (
8281

8382
const createTestCaseExecutor = async (
8483
testCaseExecution: TestCaseExecution,
85-
plugins: string[],
84+
plugins: Plugin[],
8685
testRunnerArgs?: unknown,
8786
) => {
8887
const basePath = dirname(testCaseExecution.filePath)
89-
const loadedPlugins = await Promise.all(plugins.map((plugin) => import(plugin)))
90-
const executePluginFunction = createPluginExecutor(loadedPlugins, basePath, testRunnerArgs)
88+
const executePluginFunction = createPluginExecutor(plugins, basePath, testRunnerArgs)
9189
const phaseExecutor = createPhaseExecutor(executePluginFunction, testCaseExecution)
9290
let errors: Error[] = []
9391

@@ -118,7 +116,7 @@ const createTestCaseExecutor = async (
118116

119117
export const executeTestCase = async (
120118
testCaseExecution: TestCaseExecution,
121-
plugins: string[],
119+
plugins: Plugin[],
122120
testRunnerArgs?: unknown,
123121
) => {
124122
debug(`TestCase: ${JSON.stringify(testCaseExecution, null, 2)}`)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import nock from 'nock'
22

3-
export const arrange = (): boolean => {
3+
export const arrange = async (): Promise<boolean> => {
44
nock.disableNetConnect()
55
return true
66
}

dtc/src/plugins/function-call-plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export const act = async (args: unknown, basePath: string): Promise<boolean> =>
4141
return true
4242
}
4343

44-
export const assert = (args: unknown): boolean => {
44+
export const assert = async (args: unknown): Promise<boolean> => {
4545
if (!is(args, FunctionCallResponse)) {
4646
return false
4747
}

dtc/src/plugins/http-mock-plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export const arrange = async (args: unknown): Promise<boolean> => {
6868
return true
6969
}
7070

71-
export const assert = (): boolean => {
71+
export const assert = async (): Promise<boolean> => {
7272
if (!nock.isDone()) {
7373
const pendingUrls = nock.pendingMocks()
7474
nock.cleanAll()

dtc/test/fixtures/layers.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {mock} from 'node:test'
22

3-
export const arrange = mock.fn()
4-
export const act = mock.fn(() => false)
5-
export const assert = mock.fn()
6-
export const clean = mock.fn()
3+
export const arrange = mock.fn(async () => true)
4+
export const act = mock.fn(async () => false)
5+
export const assert = mock.fn(async () => true)
6+
export const clean = mock.fn(async () => true)

0 commit comments

Comments
 (0)