Skip to content

Commit

Permalink
chore(common): migrate unit test on jest
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Jun 3, 2022
1 parent de34753 commit fbd1a58
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 44 deletions.
14 changes: 14 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// For a detailed explanation regarding each configuration property, visit:
// https://jestjs.io/docs/en/configuration.html

module.exports = {
...require("@tsed/jest-config")(__dirname, "root"),
coverageThreshold: {
global: {
statements: 99.13,
branches: 91.18,
functions: 98.87,
lines: 99.14
}
}
};
5 changes: 0 additions & 5 deletions packages/graphql/typegraphql/.mocharc.js

This file was deleted.

14 changes: 14 additions & 0 deletions packages/graphql/typegraphql/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// For a detailed explanation regarding each configuration property, visit:
// https://jestjs.io/docs/en/configuration.html

module.exports = {
...require("@tsed/jest-config")(__dirname, "typegraphql"),
coverageThreshold: {
global: {
statements: 100,
branches: 70.58,
functions: 100,
lines: 100
}
}
};
4 changes: 2 additions & 2 deletions packages/graphql/typegraphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"barrels": "yarn barrelsby --delete -d ./src -e \"\\.spec\\.ts\" -e \"__mock__\" -e \".benchmark.ts\"",
"start:express": "ts-node -r tsconfig-paths/register test/app/index.express.ts",
"start:koa": "ts-node -r tsconfig-paths/register test/app/index.koa.ts",
"test": "cross-env NODE_ENV=test nyc mocha"
"test": "cross-env NODE_ENV=test jest"
},
"dependencies": {
"@tsed/apollo": "6.114.16",
Expand All @@ -41,4 +41,4 @@
"graphql": ">=15.0.0",
"type-graphql": ">=1.0.0"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import {ApolloService} from "@tsed/apollo";
import {PlatformTest} from "@tsed/common";
import {TypeGraphQLService} from "@tsed/typegraphql";
import {expect} from "chai";
import Sinon from "sinon";
import {AuthResolver, RecipeResolver} from "../../test/app/graphql/index";

const sandbox = Sinon.createSandbox();
const noop = () => {};

function createApolloServiceFixture() {
const server = {
Expand Down Expand Up @@ -38,7 +36,6 @@ describe("TypeGraphQLService", () => {
);
afterEach(() => {
PlatformTest.reset();
sandbox.restore();
});

describe("createServer()", () => {
Expand All @@ -53,28 +50,19 @@ describe("TypeGraphQLService", () => {
}
]);

sandbox.stub(service, "createSchema");
jest.spyOn(service as any, "createSchema").mockReturnValue({schema: "schema"});

// @ts-ignore
sandbox.stub(service, "createDataSources");

const noop = () => ({});
// GIVEN
// @ts-ignore
service.createSchema.returns({schema: "schema"});
// @ts-ignore
service.createDataSources.returns(noop);
// WHEN
jest.spyOn(service as any, "createDataSources").mockReturnValue(noop);

const result1 = await service.createServer("key", {
path: "/path"
} as any);
const result2 = await service.createServer("key", {path: "/path"} as any);

expect(result2).to.deep.eq(result1);
expect(result1.server).to.deep.eq("server");
expect(service.getSchema("key")).to.deep.eq("schema");
expect(service.createSchema).to.have.been.calledOnceWithExactly({
expect(result2).toEqual(result1);
expect(result1.server).toEqual("server");
expect(service.getSchema("key")).toEqual("schema");
expect(service.createSchema).toHaveBeenCalledWith({
resolvers: [AuthResolver, RecipeResolver],
container: PlatformTest.injector
});
Expand All @@ -83,10 +71,10 @@ describe("TypeGraphQLService", () => {
});
describe("createDataSources", () => {
it("should return a function with all dataSources", () => {
const dataSources = sandbox.stub().returns({
const dataSources = jest.fn().mockReturnValue({
api: "api"
});
const serverConfigSources = sandbox.stub().returns({
const serverConfigSources = jest.fn().mockReturnValue({
api2: "api2"
});

Expand All @@ -96,7 +84,7 @@ describe("TypeGraphQLService", () => {
const fn = service.createDataSources(dataSources, serverConfigSources);
const result = fn();

expect(result).to.deep.eq({
expect(result).toEqual({
api2: "api2",
api: "api",
myDataSource: result.myDataSource
Expand All @@ -109,7 +97,7 @@ describe("TypeGraphQLService", () => {
const fn = service.createDataSources();
const result = fn();

expect(result).to.deep.eq({
expect(result).toEqual({
myDataSource: result.myDataSource
});
});
Expand Down
11 changes: 5 additions & 6 deletions packages/graphql/typegraphql/test/graphql-passport.spec.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import {PlatformTest} from "@tsed/common";
import {PlatformExpress} from "@tsed/platform-express";
import "@tsed/platform-express";
import {expect} from "chai";
import SuperTest from "supertest";
import {Server} from "./app/Server";

describe("GraphQL", () => {
let request: SuperTest.SuperTest<SuperTest.Test>;
before(
beforeAll(
PlatformTest.bootstrap(Server, {
platform: PlatformExpress
})
);
before(() => {
beforeAll(() => {
request = SuperTest(PlatformTest.callback());
});
after(PlatformTest.reset);
afterAll(PlatformTest.reset);

it("should try login", async () => {
const response = await request
Expand All @@ -27,7 +26,7 @@ describe("GraphQL", () => {
})
.expect(200);

expect(response.body).to.deep.eq({
expect(response.body).toEqual({
data: {
login: {
email: "test@test.com",
Expand All @@ -47,7 +46,7 @@ describe("GraphQL", () => {
})
.expect(200);

expect(response.body).to.deep.eq({
expect(response.body).toEqual({
data: null,
errors: [
{
Expand Down
9 changes: 4 additions & 5 deletions packages/graphql/typegraphql/test/graphql.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {PlatformTest} from "@tsed/common";
import "@tsed/platform-express";
import {PlatformExpress} from "@tsed/platform-express";
import {ApolloServerTestClient, createTestClient} from "apollo-server-testing";
import {expect} from "chai";
import gql from "graphql-tag";
import {TypeGraphQLService} from "../src";
import {Server} from "./app/Server";
Expand All @@ -19,24 +18,24 @@ const GET_RECIPES = gql`

describe("GraphQL", () => {
let request: ApolloServerTestClient;
before(
beforeAll(
PlatformTest.bootstrap(Server, {
platform: PlatformExpress
})
);
before(() => {
beforeAll(() => {
const server = PlatformTest.get<TypeGraphQLService>(TypeGraphQLService).get("default")!;
request = createTestClient(server as any);
});
after(PlatformTest.reset);
afterAll(PlatformTest.reset);

it("should get recipes", async () => {
const response = await request.query({
query: GET_RECIPES,
variables: {}
});

expect(response.data).to.deep.eq({
expect(response.data).toEqual({
recipes: [
{
creationDate: "2020-08-20T00:00:00.000Z",
Expand Down
18 changes: 15 additions & 3 deletions tools/jest/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,17 @@ module.exports = (rootDir) => ({

// moduleDirectories: ["node_modules", "packages"],
// An array of regexp pattern strings used to skip coverage collection
coveragePathIgnorePatterns: ["index.ts", "/node_modules/", "/test/", "exports.ts", "__mock__"],
coveragePathIgnorePatterns: [
"index.ts",
"/node_modules/",
"/test/",
"exports.ts",
"__mock__",
"platform-test-utils",
"engines",
"platform-express",
"platform-koa"
],
moduleNameMapper: {
"^@tsed/core$": fixPath(join(packageDir, "core/src")),
"^@tsed/di$": fixPath(join(packageDir, "di/src")),
Expand All @@ -33,6 +43,8 @@ module.exports = (rootDir) => ({
"^@tsed/json-mapper$": fixPath(join(packageDir, "specs/json-mapper/src")),
"^@tsed/openspec$": fixPath(join(packageDir, "specs/openspec/src")),
"^@tsed/swagger$": fixPath(join(packageDir, "specs/swagger/src")),
"^@tsed/apollo": fixPath(join(packageDir, "graphql/apollo/src")),
"^@tsed/typegraphql": fixPath(join(packageDir, "graphql/typegraphql/src")),
"^@tsed/platform-(.*)$": fixPath(join(packageDir, "platform/platform-$1/src")),
"^@tsed/testing-mongoose$": fixPath(join(packageDir, "orm/testing-mongoose/src")),
"^@tsed/objection$": fixPath(join(packageDir, "orm/objection/src")),
Expand All @@ -54,8 +66,8 @@ module.exports = (rootDir) => ({
"^@tsed/jwks$": fixPath(join(packageDir, "security/jwks/src")),
"^@tsed/oidc-provider$": fixPath(join(packageDir, "security/oidc-provider/src"))
},
modulePathIgnorePatterns: ["<rootDir>/lib", "<rootDir>/dist"],
// An object that configures minimum threshold enforcement for coverage results
testPathIgnorePatterns: ["node_modules", "/docs/", "/docs-references/", "/platform-express/", "/platform-koa/", "/engines/"],
modulePathIgnorePatterns: ["<rootDir>/lib", "<rootDir>/dist"], // An object that configures minimum threshold enforcement for coverage results
coverageThreshold: {
global: {
branches: 70,
Expand Down

0 comments on commit fbd1a58

Please sign in to comment.