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

migrating unit tests to native note:testing #173

Closed
wants to merge 8 commits into from
Closed
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
24 changes: 0 additions & 24 deletions .babelrc

This file was deleted.

4 changes: 2 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 20.x
node-version: 23.x

- name: Run build and tests
run: |
npm ci
npm run test
npm run test:full

- name: Archive mutation reports
if: ${{ failure() }}
Expand Down
9,097 changes: 2,653 additions & 6,444 deletions package-lock.json

Large diffs are not rendered by default.

16 changes: 5 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,23 @@
"module": "./dist/esm/index.js",
"types": "./dist/esm/index.d.ts",
"devDependencies": {
"@babel/core": "^7.26.0",
"@babel/plugin-proposal-class-properties": "^7.18.6",
"@babel/plugin-proposal-decorators": "^7.25.9",
"@babel/preset-env": "^7.26.0",
"@babel/preset-typescript": "^7.26.0",
"@stryker-mutator/core": "^8.6.0",
"@stryker-mutator/jest-runner": "^8.6.0",
"@stryker-mutator/typescript": "^4.0.0",
"@types/jest": "^29.5.14",
"@types/node": "^20.10.5",
"@types/node": "^22.8.6",
"@typescript-eslint/eslint-plugin": "^6.15.0",
"@typescript-eslint/parser": "^6.15.0",
"babel-jest": "^29.7.0",
"coveralls": "^3.1.1",
"eslint": "^8.56.0",
"eslint-config-airbnb-typescript": "^17.1.0",
"eslint-plugin-import": "^2.29.1",
"jest": "^29.7.0",
"tsx": "^4.19.2",
"typescript": "^5.6.3"
},
"scripts": {
"test": "npm run clean && tsc -p tsconfig.test.json --noEmit && npm run lint && jest --coverage && npm run test:mutation",
"test:unit": "npm run clean && tsc --noEmit && npm run lint && jest --clearCache && jest",
"test:full": "npm run clean && tsc -p tsconfig.test.json --noEmit && npm run lint && node --test --experimental-test-coverage && npm run test:mutation",
"test:unit": "npm run clean && tsc --noEmit && npm run lint && node --test",
"test": "tsx --test $(find src -type f -name 'cancel-previous.spec.ts')",
"test:mutation": "stryker run",
"lint": "eslint ./src --ext .ts --quiet",
"lint:fix": "eslint ./src --ext .ts --fix",
Expand Down
167 changes: 62 additions & 105 deletions src/after/after.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { after } from './after';
import { AfterFunc, AfterParams } from './after.model';
import { describe, it, mock } from 'node:test';
import assert from 'node:assert';
import { assertCalls } from '../common/test-utils';
import { AfterParams } from './after.model';

describe('after', () => {
it('should make sure error thrown when decorator not set on method', () => {
try {
const nonValidAfter: any = after({ func: null });

class T {
@nonValidAfter
boo: string;
@nonValidAfter boo: string;
}
} catch (e) {
expect('@after is applicable only on a methods.').toBe(e.message);
assert.equal(e.message, '@after is applicable only on a methods.');

return;
}
Expand All @@ -20,15 +22,10 @@ describe('after', () => {
});

it('should verify after method invocation with the right context when provided as string', () => {
let counter = 0;

class T {
prop: number;

after(): void {
expect(this.prop).toBe(3);
expect(counter).toBe(1);
}
after = mock.fn();

@after<T, void>({
func: 'after',
Expand All @@ -37,90 +34,80 @@ describe('after', () => {
return this.goo(x);
}

goo(x: number): void {
expect(this.prop).toBe(3);
expect(counter).toBe(0);
counter += 1;
}
goo = mock.fn();
}

const t = new T();
t.prop = 3;
const spyGoo = jest.spyOn(T.prototype, 'goo');
const spyAfter = jest.spyOn(T.prototype, 'after');

t.foo(1);
expect(spyGoo).toHaveBeenCalledTimes(1);
expect(spyGoo).toHaveBeenCalledWith(1);
expect(spyAfter).toHaveBeenCalledTimes(1);
assertCalls(t.goo, [
[[1]],
]);

assertCalls(t.after, [
[
[
{
args: [1],
response: undefined,
},
],
],
]);
});

it('should verify after method invocation when method is provided', () => {
let counter = 0;

const afterFunc = jest.fn(() => {
expect(counter).toBe(1);
});
it('should verify after method invocation when function is provided', () => {
const afterFunc = mock.fn();

class T {
@after<T, void>({
func: afterFunc,
})
foo(x: number): void {
return this.goo(x);
}

goo(x: number): void {
expect(counter).toBe(0);
counter += 1;
foo(x: number): number {
return 2;
}
}

const t = new T();
const spyGoo = jest.spyOn(T.prototype, 'goo');

t.foo(1);
expect(spyGoo).toHaveBeenCalledTimes(1);
expect(spyGoo).toHaveBeenCalledWith(1);
expect(afterFunc.mock.calls.length).toBe(1);
assertCalls(afterFunc, [
[[{ args: [1], response: 2 }]],
]);
});

it('should verify after method invocation when method is provided without waiting for it to be resolved', (done) => {
let counter = 0;

const afterFunc = jest.fn(() => {
expect(counter).toBe(1);
counter += 1;
it('should verify after method invocation when method is provided without waiting for it to be resolved', async () => {
const mocker = mock.fn();
const afterFunc = mock.fn((args: AfterParams) => {
mocker(2);
});

class T {
@after<T, void>({
func: afterFunc,
func: afterFunc as any,
})
foo(x: number): Promise<void> {
expect(counter).toBe(0);
counter += 1;

return new Promise((resolve) => {
setTimeout(() => {
expect(counter).toBe(2);
mocker(1);
resolve();
done();
}, 0);
});
}
}

const t = new T();
t.foo(1);
await t.foo(1);
assertCalls(mocker, [
[[2]],
[[1]],
]);
});

it('should verify after method invocation when method is provided with waiting for it to be resolved', (done) => {
let counter = 0;

const afterFunc = jest.fn(() => {
expect(counter).toBe(2);
done();
it('should verify after method invocation when method is provided with waiting for it to be resolved', async () => {
const mocker = mock.fn();
const afterFunc = mock.fn((args: AfterParams) => {
mocker(2);
});

class T {
Expand All @@ -129,34 +116,25 @@ describe('after', () => {
wait: true,
})
foo(): Promise<void> {
expect(counter).toBe(0);
counter += 1;

return new Promise((resolve) => {
setTimeout(() => {
expect(counter).toBe(1);
counter += 1;
mocker(1);
resolve();
}, 0);
});
}
}

const t = new T();
t.foo();
await t.foo();
assertCalls(mocker, [
[[1]],
[[2]],
]);
});

it('should provide args and response to after method - sync', (done) => {
const testable1 = 5;
const testable2 = 6;

const afterFunc: AfterFunc<number> = (x: AfterParams<number>): void => {
expect(x.args.length).toBe(2);
expect(x.args[0]).toBe(testable1);
expect(x.args[1]).toBe(testable2);
expect(x.response).toBe(testable1 + testable2);
done();
};
it('should provide args and response to after method - sync', () => {
const afterFunc = mock.fn();

class T {
@after<T, number>({
Expand All @@ -168,19 +146,15 @@ describe('after', () => {
}

const t = new T();
t.foo(testable1, testable2);
t.foo(1, 2);

assertCalls(afterFunc, [
[[{ args: [1, 2], response: 3 }]],
]);
});

it('should provide args and response to after method - async', async () => {
const testable1 = 5;
const testable2 = 6;

const afterFunc: AfterFunc<number> = (x: AfterParams<number>): void => {
expect(x.args.length).toBe(2);
expect(x.args[0]).toBe(testable1);
expect(x.args[1]).toBe(testable2);
expect(x.response).toBe(testable1 + testable2);
};
const afterFunc = mock.fn();

class T {
@after({
Expand All @@ -193,27 +167,10 @@ describe('after', () => {
}

const t = new T();
await t.foo(testable1, testable2);
});

it('should returned result of method', async () => {
const value = 42;

const afterFunc: AfterFunc<number> = jest.fn((x: AfterParams<number>) => {
expect(x.response).toBe(value);
});
await t.foo(1, 2);

class T {
@after<T>({
func: afterFunc,
})
foo(x: number): number {
return x;
}
}

const t = new T();
const result = await t.foo(value);
expect(result).toBe(value);
assertCalls(afterFunc, [
[[{ args: [1, 2], response: 3 }]],
]);
});
});
2 changes: 1 addition & 1 deletion src/after/after.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function after<T = any, D = any>(config: AfterConfig<T, D>): Decorator<T>
propertyName: keyof T,
descriptor: TypedPropertyDescriptor<Method<any>>,
): TypedPropertyDescriptor<Method<D>> => {
if (descriptor.value) {
if (descriptor && descriptor.value) {
descriptor.value = afterify(descriptor.value, config);

return descriptor;
Expand Down
Loading
Loading