Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assert exception #25

Merged
merged 5 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 11 additions & 3 deletions dtc/src/domain.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
export type Loader = (filePath: string) => Promise<TestCase>

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

export type GenericAttributes = {
[key: string]: string | number | boolean | Record<string, unknown> | Record<string, unknown>[]
Expand All @@ -11,11 +16,14 @@ export type TestCase = GenericAttributes & {
debug?: boolean
retry?: number
delay?: number
timeout?: number
timeout?: number
parameters?: Record<string, unknown> | Record<string, unknown>[]
arrange?: Record<string, unknown>
act?: Record<string, unknown>
assert?: unknown
assert?: {
exception?: unknown
[x: string]: unknown
}
clean?: Record<string, unknown>
}

Expand Down
14 changes: 12 additions & 2 deletions dtc/src/plugins/function-call-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import extraAssert from '@cgauge/assert'
import {isRecord} from '../utils.js'
import nodeAssert from 'node:assert'

type FunctionCallAct = {
import: string
Expand All @@ -10,6 +11,7 @@ type FunctionCallAct = {
const isFunctionCallAct = (v: unknown): v is FunctionCallAct => typeof v === 'object' && v !== null && 'import' in v

let response: any
let exception: any

export const act = async (args: unknown, basePath: string) => {
if (!isFunctionCallAct(args)) {
Expand All @@ -24,10 +26,18 @@ export const act = async (args: unknown, basePath: string) => {
}
/* c8 ignore end */

response = await module[args.import].apply(null, args.arguments)
try {
response = await module[args.import].apply(null, args.arguments)
} catch (e) {
exception = e
}
}

export const assert = (args: unknown) => {
export const assert = (args: {exception?: {name?: string}; [x: string]: unknown}) => {
if (args.exception?.name) {
nodeAssert.equal(args?.exception?.name, exception.name)
}

if (response && isRecord(args) && args) {
extraAssert.objectContains(response, args)
}
Expand Down
4 changes: 4 additions & 0 deletions dtc/test/fixtures/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ export const noArgs = () => true
export const syncFunction = (args) => args

export const asyncFunction = async (args) => args

export const functionWithException = (__args) => {
throw new Error()
}
50 changes: 31 additions & 19 deletions dtc/test/plugins/function-call-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,47 @@ import {fileURLToPath} from 'node:url'

const __dirname = dirname(fileURLToPath(import.meta.url))

test('It call a sync function without args', async () => {
await act({
import: 'noArgs',
from: '../fixtures/functions.js',
}, __dirname)

assert(true)
})

test('It call a sync function with args', async () => {
const args = {a: 'b'}

await act({
import: 'syncFunction',
from: '../fixtures/functions.js',
arguments: [args],
}, __dirname)
await act(
{
import: 'syncFunction',
from: '../fixtures/functions.js',
arguments: [args],
},
__dirname,
)

assert(args)
})

test('It call a async function with args', async () => {
const args = {a: 'b'}

await act({
import: 'asyncFunction',
from: '../fixtures/functions.js',
arguments: [args],
}, __dirname)
await act(
{
import: 'asyncFunction',
from: '../fixtures/functions.js',
arguments: [args],
},
__dirname,
)

assert(args)
})

test('It call a sync function with exception', async () => {
const args = {exception: {name: 'Error'}}

await act(
{
import: 'functionWithException',
from: '../fixtures/functions.js',
arguments: [args],
},
__dirname,
)

assert(args)
})