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 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
2 changes: 1 addition & 1 deletion assert/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cgauge/assert",
"version": "0.8.2",
"version": "0.9.0",
"description": "Extra assert library",
"type": "module",
"repository": {
Expand Down
8 changes: 4 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.8.2",
"version": "0.9.0",
"description": "AWS plugin for Declarative TestCases",
"repository": {
"type": "git",
Expand All @@ -21,9 +21,9 @@
"@aws-sdk/client-sns": "^3.645.0",
"@aws-sdk/lib-dynamodb": "^3.645.0",
"@aws-sdk/util-dynamodb": "^3.645.0",
"@cgauge/assert": "^0.8.0",
"@cgauge/dtc": "^0.8.0",
"@cgauge/nock-aws": "^0.8.0"
"@cgauge/assert": "^0.9.0",
"@cgauge/dtc": "^0.9.0",
"@cgauge/nock-aws": "^0.9.0"
},
"scripts": {
"build": "rm -rf dist && tsc",
Expand Down
6 changes: 3 additions & 3 deletions dtc-graphql-plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cgauge/dtc-graphql-plugin",
"version": "0.8.2",
"version": "0.9.0",
"description": "GraphQl plugin for Declarative TestCases",
"repository": {
"type": "git",
Expand All @@ -15,8 +15,8 @@
"author": "Salam Suleymanov",
"license": "LGPL-3.0-or-later",
"dependencies": {
"@cgauge/assert": "^0.8.0",
"@cgauge/dtc": "^0.8.0",
"@cgauge/assert": "^0.9.0",
"@cgauge/dtc": "^0.9.0",
"graphql-request": "^7.1.2"
},
"scripts": {
Expand Down
6 changes: 3 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.8.2",
"version": "0.9.0",
"description": "MySQL plugin for Declarative TestCases",
"repository": {
"type": "git",
Expand All @@ -15,8 +15,8 @@
"author": "Abdala Cerqueira",
"license": "LGPL-3.0-or-later",
"dependencies": {
"@cgauge/assert": "^0.8.0",
"@cgauge/dtc": "^0.8.0",
"@cgauge/assert": "^0.9.0",
"@cgauge/dtc": "^0.9.0",
"mysql2": "^3.11.0",
"node-sql-parser": "^5.1.0"
},
Expand Down
6 changes: 3 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.8.2",
"version": "0.9.0",
"description": "Playwright plugin for Declarative TestCases",
"repository": {
"type": "git",
Expand All @@ -15,8 +15,8 @@
"author": "Abdala Cerqueira",
"license": "LGPL-3.0-or-later",
"dependencies": {
"@cgauge/assert": "^0.8.0",
"@cgauge/dtc": "^0.8.0",
"@cgauge/assert": "^0.9.0",
"@cgauge/dtc": "^0.9.0",
"@playwright/test": "^1.47.0"
},
"scripts": {
Expand Down
4 changes: 2 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.8.2",
"version": "0.9.0",
"description": "Declarative TestCases",
"repository": {
"type": "git",
Expand All @@ -18,7 +18,7 @@
"author": "Abdala Cerqueira",
"license": "LGPL-3.0-or-later",
"dependencies": {
"@cgauge/assert": "^0.8.0",
"@cgauge/assert": "^0.9.0",
"cleye": "^1.3.2",
"nock": "^14.0.0-beta.15"
},
Expand Down
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)
})
2 changes: 1 addition & 1 deletion nock-aws/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cgauge/nock-aws",
"version": "0.8.2",
"version": "0.9.0",
"description": "AWS Request mocker based on Nock",
"repository": {
"type": "git",
Expand Down
36 changes: 18 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion yaml/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cgauge/yaml",
"version": "0.8.2",
"version": "0.9.0",
"description": "YAML parser with extra tags",
"repository": {
"type": "git",
Expand Down