Skip to content

Commit

Permalink
refactor: migrated engine over
Browse files Browse the repository at this point in the history
  • Loading branch information
Raymond Ottun committed Sep 25, 2021
1 parent aeb6b9f commit 311a527
Show file tree
Hide file tree
Showing 25 changed files with 2,296 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ curl -X PUT http://localhost:8080/_/api/requests/sequence -H "content-type:appli
]'
```

see the [Behavior Guide](http://sayjava.github.com/deputy)
see the [Mock Guide](http://sayjava.github.com/deputy)

### Programmatically Use cases (Express Middleware / NodeJS HTTP Middleware)

Expand Down
4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
18 changes: 11 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,27 @@
"test": "jest"
},
"devDependencies": {
"@types/express": "^4.17.9",
"@types/jest": "^26.0.15",
"@types/node": "^14.14.7",
"@types/supertest": "^2.0.10",
"@types/yargs": "^15.0.10",
"@typescript-eslint/eslint-plugin": "^4.11.1",
"@typescript-eslint/parser": "^4.11.1",
"eslint": "^7.17.0",
"eslint-config-prettier": "^7.1.0",
"eslint-plugin-prettier": "^3.3.0",
"prettier": "^2.2.1",
"vuepress": "^1.5.3",
"@types/express": "^4.17.9",
"@types/node": "^14.14.7",
"@types/supertest": "^2.0.10",
"@types/yargs": "^15.0.10",
"jest": "^26.6.3",
"nodemon": "^2.0.6",
"prettier": "^2.2.1",
"supertest": "^6.0.1",
"ts-jest": "^26.4.4",
"ts-node": "^9.0.0",
"typescript": "^4.0.5"
"typescript": "^4.0.5",
"vuepress": "^1.5.3"
},
"dependencies": {
"axios": "^0.21.4",
"shortid": "^2.2.16"
}
}
328 changes: 328 additions & 0 deletions src/engine/assert.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,328 @@
import { create } from './engine';
import { Mock, Request, Verification, VerificationError } from '../types';

test('at most 1 time and at least 1 time', async () => {
const mocks: Mock[] = [
{
id: 'exp1',
name: 'sample1',
request: {
headers: {},
path: '/todos',
method: 'GET',
},
response: {
statusCode: 200,
},
},
{
id: 'exp1',
name: 'sample1',
request: {
headers: {},
path: '/todos',
method: 'POST',
},
response: {
statusCode: 200,
},
},
];

const engine = create({ mocks, config: {} });

const post: Request = {
path: '/todos',
method: 'POST',
headers: {},
};

const get: Request = {
path: '/todos',
method: 'GET',
headers: {},
};

await engine.execute(get);
await engine.execute(get);

const verificationPost: Verification = {
request: post,
limit: { atMost: 1 },
};
const verifiedPost = engine.assert(verificationPost);
expect(verifiedPost).toMatchInlineSnapshot(`true`);

const verificationGet: Verification = {
request: get,
limit: { atLeast: 1 },
};
const verifiedGet = engine.assert(verificationGet);
expect(verifiedGet).toMatchInlineSnapshot(`true`);
});

test('exactly 2 times', async () => {
const mocks: Mock[] = [
{
id: 'exp1',
name: 'sample1',
request: {
headers: {},
path: '/todos',
method: 'GET',
},
response: {
statusCode: 200,
},
},
];

const engine = create({ mocks, config: {} });

const request: Request = {
path: '/todos',
method: 'GET',
headers: {},
};

await engine.execute(request);
await engine.execute(request);
await engine.execute(request);
await engine.execute(request);

const verification: Verification = {
request,
limit: { atMost: 2, atLeast: 2 },
};
const verified = engine.assert(verification) as VerificationError;

expect(verified.message).toMatchInlineSnapshot(
`"Expected to have received GET:/todos at most 2 times but was received 4 times"`,
);
});

test('matches at least once', async () => {
const mocks: Mock[] = [
{
id: 'exp1',
name: 'sample1',
request: {
headers: {},
path: '/todo/2',
method: 'GET',
},
response: {
statusCode: 200,
},
limit: 1,
},
];

const engine = create({ mocks, config: {} });

const request: Request = {
path: '/todo/2',
method: 'GET',
headers: {},
};

const verification: Verification = {
request,
};

await engine.execute(request);
const verified = engine.assert(verification);

expect(verified).toMatchInlineSnapshot(`true`);
});

test('at least 1 times with other records', async () => {
const mocks: Mock[] = [
{
id: 'exp1',
name: 'sample1',
request: {
path: '/todos$',
method: 'GET',
},
response: {
statusCode: 200,
},
limit: 1,
},
];

const engine = create({ mocks, config: {} });

const request: Request = {
path: '/todos/take-trash-out',
method: 'GET',
};

await engine.execute(request);
await engine.execute(request);
await engine.execute(request);
await engine.execute(request);

const verification: Verification = { request, limit: { atLeast: 1 } };
const verified = engine.assert(verification) as VerificationError;

expect(verified.message).toMatchInlineSnapshot(
`"Expected to have received GET:/todos/take-trash-out at least 1 times but was received 0 times"`,
);
});

test('assert 3 counts on a 2 limit response', async () => {
const mocks: Mock[] = [
{
id: 'exp1',
name: 'sample1',
request: {
headers: {},
path: '/happy_feet',
method: 'GET',
},
response: {
statusCode: 200,
},
limit: 2,
},
];

const engine = create({ mocks, config: {} });

const request: Request = {
path: '/happy_feet',
method: 'GET',
headers: {},
};

await engine.execute(request);
await engine.execute(request);
await engine.execute(request);
await engine.execute(request);
await engine.execute(request);

const verification: Verification = {
request,
limit: { atLeast: 3 },
};
const verified = engine.assert(verification) as VerificationError;

expect(verified.message).toMatchInlineSnapshot(
`"Expected to have received GET:/happy_feet at least 3 times but was received 2 times"`,
);
});

test('at most 3 times', async () => {
const mocks: Mock[] = [
{
id: 'exp1',
name: 'sample1',
request: {
headers: {},
path: '/todos',
method: 'GET',
},
response: {
statusCode: 200,
},
},
];

const engine = create({ mocks, config: {} });

const request: Request = {
path: '/todos',
method: 'GET',
headers: {},
};

await engine.execute(request);
await engine.execute(request);
await engine.execute(request);
await engine.execute(request);

const verification: Verification = { request, limit: { atMost: 3 } };
const verified = engine.assert(verification) as VerificationError;

expect(verified.message).toMatchInlineSnapshot(
`"Expected to have received GET:/todos at most 3 times but was received 4 times"`,
);
});

test('at least 3 times', async () => {
const mocks: Mock[] = [
{
id: 'exp1',
name: 'sample1',
request: {
headers: {},
path: '/todos',
method: 'GET',
},
response: {
statusCode: 200,
},
},
];

const engine = create({ mocks, config: {} });

const request: Request = {
path: '/todos',
method: 'GET',
headers: {},
};

await engine.execute(request);
await engine.execute(request);
await engine.execute(request);
await engine.execute(request);

const verification: Verification = { request, limit: { atLeast: 3 } };
const verified = engine.assert(verification);

expect(verified).toMatchInlineSnapshot(`true`);
});

test('empty record matches', () => {
const mocks: Mock[] = [
{
id: 'exp1',
name: 'sample1',
request: {
headers: {},
path: '/todos',
method: 'GET',
},
response: {
statusCode: 200,
},
limit: 1,
},
];

const engine = create({ mocks, config: {} });

const request: Request = {
path: '/todos',
method: 'GET',
headers: {},
};

const verification: Verification = {
request,
};

const verified = engine.assert(verification);

expect(verified).toMatchInlineSnapshot(`
Object {
"actual": 0,
"expected": 1,
"message": "Expected to have received GET:/todos at least 1 times but was received 0 times",
"records": Array [],
}
`);
});

0 comments on commit 311a527

Please sign in to comment.