Skip to content

Commit

Permalink
add integration tests (subquery#96)
Browse files Browse the repository at this point in the history
* add integration test and support run test in docker-compose

* apply eslint to tests

* run all tests in pr workflow

* --abort-on-container-exit

* remove --build by default

* set moduleNameMapper

* --forceExit

* --detectOpenHandles
  • Loading branch information
ianhe8x authored Jan 19, 2021
1 parent 4137a0e commit bc770be
Show file tree
Hide file tree
Showing 18 changed files with 182 additions and 28 deletions.
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
.github
node_modules
coverage
deploy
docs
test/docker-compose.yaml
test/Dockerfile
1 change: 0 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
/api_docs/**
**/node_modules/**
**/test/**/*
**/*.spec.*
/scripts/*
packages/**/dist/**
packages/**/lib/**
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: code style check
run: yarn pretty-quick --check --pattern 'packages/**/src/**/*'
- name: lint
run: yarn eslint packages --ext .ts
run: yarn lint
- name: test
run: yarn test
run: yarn test:docker

11 changes: 10 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ module.exports = {

// A set of global variables that need to be available in all test environments
// globals: {},
globals: {
'ts-jest': {
tsconfig: 'tsconfig.test.json',
},
},

// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
// maxWorkers: "50%",
Expand All @@ -84,6 +89,10 @@ module.exports = {

// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
// moduleNameMapper: {},
moduleNameMapper: {
"^@subql/common": '<rootDir>/packages/common/src',
"^@subql/common/(.*)$": '<rootDir>/packages/common/src/$1',
},

// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
// modulePathIgnorePatterns: [],
Expand Down Expand Up @@ -179,7 +188,7 @@ module.exports = {

// A map from regular expressions to paths to transformers
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
"^.+\\.ts$": "ts-jest"
},

// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
"scripts": {
"docs:dev": "vuepress dev docs",
"docs:build": "vuepress build docs",
"test": "jest --coverage"
"lint": "eslint packages --ext .ts",
"test": "jest --coverage",
"test:all": "jest --coverage --testRegex='.*\\.(spec|test)\\.ts$' --forceExit --detectOpenHandles",
"test:docker": "docker-compose -f test/docker-compose.yaml up --remove-orphans --abort-on-container-exit test"
},
"husky": {
"hooks": {
Expand Down
15 changes: 9 additions & 6 deletions packages/cli/src/controller/init-controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
//Test create starter project
import {createProject} from './init-controller';
// Copyright 2020-2021 OnFinality Limited authors & contributors
// SPDX-License-Identifier: Apache-2.0

import * as fs from 'fs';
const projectName = 'mockStarterProject';
import rimraf from 'rimraf';
import {createProject} from './init-controller';

const projectName = 'mockStarterProject';

// async
const fileExists = (file) => {
const fileExists = async (file) => {
return new Promise((resolve, reject) => {
fs.access(file, fs.constants.F_OK, (err) => {
err ? reject(err) : resolve(true);
Expand All @@ -24,11 +27,11 @@ describe('Cli can create project', () => {

it('should resolves when starter project successful created', async () => {
await createProject(projectName);
await expect(fileExists(`./${projectName}`)).resolves;
await expect(fileExists(`./${projectName}`)).resolves.toEqual(true);
});

it('throw error if same name directory exists', async () => {
await fs.mkdirSync(`./${projectName}`);
fs.mkdirSync(`./${projectName}`);
await expect(createProject(projectName)).rejects.toThrow();
});

Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/graphql/graphql.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// SPDX-License-Identifier: Apache-2.0

import gql from 'graphql-tag';
import {buildSchemaFromDocumentNode} from './schema';
import {getAllEntities} from './entities';
import {buildSchemaFromDocumentNode} from './schema';

describe('utils that handle schema.graphql', () => {
it('support @entity annotation in the schema', () => {
Expand Down
8 changes: 1 addition & 7 deletions packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nodemon",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
"start:prod": "node dist/main"
},
"dependencies": {
"@nestjs/common": "^7.6.1",
Expand Down
3 changes: 3 additions & 0 deletions packages/node/src/configure/NodeConfig.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2020-2021 OnFinality Limited authors & contributors
// SPDX-License-Identifier: Apache-2.0

import * as path from 'path';
import { NodeConfig } from './NodeConfig';

Expand Down
55 changes: 55 additions & 0 deletions packages/node/src/db/db.module.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2020-2021 OnFinality Limited authors & contributors
// SPDX-License-Identifier: Apache-2.0

import { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import { Sequelize } from 'sequelize';
import { SubqueryRepo } from '../entities';
import { DbModule } from './db.module';

describe('DbModule', () => {
let app: INestApplication;

afterEach(async () => {
return app?.close();
});

it('can connect to database', async () => {
const module = await Test.createTestingModule({
imports: [
DbModule.forRoot({
host: process.env.DB_HOST ?? '127.0.0.1',
port: process.env.DB_PORT ? Number(process.env.DB_PORT) : 5432,
username: process.env.DB_USER ?? 'postgres',
password: process.env.DB_PASS ?? 'postgres',
database: process.env.DB_DATABASE ?? 'postgres',
}),
],
}).compile();

app = module.createNestApplication();
await app.init();
const sequelize = app.get(Sequelize);
await expect(sequelize.authenticate()).resolves.not.toThrow();
});

it('can load subquery model', async () => {
const module = await Test.createTestingModule({
imports: [
DbModule.forRoot({
host: process.env.DB_HOST ?? '127.0.0.1',
port: process.env.DB_PORT ? Number(process.env.DB_PORT) : 5432,
username: process.env.DB_USER ?? 'postgres',
password: process.env.DB_PASS ?? 'postgres',
database: process.env.DB_DATABASE ?? 'postgres',
}),
DbModule.forFeature(['Subquery']),
],
}).compile();

app = module.createNestApplication();
await app.init();
const subqueryRepo: SubqueryRepo = app.get('Subquery');
await expect(subqueryRepo.describe()).resolves.toBeTruthy();
});
});
4 changes: 0 additions & 4 deletions packages/node/src/db/db.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ export class DbModule {
logging: argv.debug,
}),
},
{
provide: 'DB_OPTION',
useValue: option,
},
],
exports: [Sequelize],
};
Expand Down
41 changes: 41 additions & 0 deletions packages/node/src/indexer/api.service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2020-2021 OnFinality Limited authors & contributors
// SPDX-License-Identifier: Apache-2.0

import { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import { SubqueryProject } from '../configure/project.model';
import { ApiService } from './api.service';

function testSubqueryProject(): SubqueryProject {
const project = new SubqueryProject();
project.network = {
endpoint: 'wss://polkadot.api.onfinality.io/public-ws',
customTypes: {
TestType: 'u32',
},
};
return project;
}

describe('ApiService', () => {
let app: INestApplication;

afterEach(async () => {
return app?.close();
});

it('can instantiate api', async () => {
const module = await Test.createTestingModule({
providers: [
{ provide: SubqueryProject, useFactory: testSubqueryProject },
ApiService,
],
}).compile();

app = module.createNestApplication();
await app.init();
const apiService = app.get(ApiService);
const api = await apiService.getApi();
expect(api.registry.getDefinition('TestType')).toEqual('u32');
});
});
2 changes: 1 addition & 1 deletion packages/node/src/indexer/sandbox.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright 2020-2021 OnFinality Limited authors & contributors
// SPDX-License-Identifier: Apache-2.0

import { IndexerSandbox } from './sandbox';
import * as path from 'path';
import { IndexerSandbox } from './sandbox';

describe('sandbox for subql-node', () => {
let vm: IndexerSandbox;
Expand Down
10 changes: 6 additions & 4 deletions packages/node/src/utils/project.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// Copyright 2020-2021 OnFinality Limited authors & contributors
// SPDX-License-Identifier: Apache-2.0

import fs from 'fs';
import { prepareProjectDir } from './project';

let projectPath = __dirname + '/../../test/projectFixture';
let tarPath =
__dirname + '/../../test/projectFixture/mockedSubqueryProject.tgz';
let badFormatPath = __dirname + '/../../test/projectFixture/bad.json';
const projectPath = `${__dirname}/../../test/projectFixture`;
const tarPath = `${__dirname}/../../test/projectFixture/mockedSubqueryProject.tgz`;
const badFormatPath = `${__dirname}/../../test/projectFixture/bad.json`;

it('Test path is a directory, outcome is same directory', async () => {
const finalPath = await prepareProjectDir(projectPath);
Expand Down
3 changes: 3 additions & 0 deletions packages/node/src/utils/promise.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright 2020-2021 OnFinality Limited authors & contributors
// SPDX-License-Identifier: Apache-2.0

import { delay } from './promise';

it('utils.promise delay()', async () => {
Expand Down
5 changes: 5 additions & 0 deletions test/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM node:14
WORKDIR /workdir

COPY . .
RUN yarn
28 changes: 28 additions & 0 deletions test/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
version: '3'

services:
postgres:
image: postgres:12-alpine
ports:
- 5432:5432
environment:
POSTGRES_PASSWORD: postgres

test:
build:
context: ..
dockerfile: test/Dockerfile
volumes:
- ../coverage:/workdir/coverage
depends_on:
- "postgres"
environment:
DB_USER: postgres
DB_PASS: postgres
DB_DATABASE: postgres
DB_HOST: postgres
DB_POST: 5432
command:
- yarn
- test:all

8 changes: 8 additions & 0 deletions tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"exclude": [
"**/node_modules/**",
"**/*.spec.ts",
"**/*.test.ts"
]
}

0 comments on commit bc770be

Please sign in to comment.