PR-448199:

Adding Testing with Jest and bin command for npx.
This commit is contained in:
Alejandro Lembke Barrientos 2022-05-25 23:39:38 +00:00
parent b55b0f61a9
commit e9bc4f091f
5 changed files with 7342 additions and 1 deletions

29
bin/cli.js Normal file
View File

@ -0,0 +1,29 @@
#!/usr/bin/env node
const { execSync } = require('child_process');
const runCommand = command => {
try{
execSync(`${command}`, {stdio: 'inherit'});
} catch (e) {
console.error(`Failed to execute ${command}`, e);
return false;
}
return true;
}
const repoName = process.argv[2];
const gitCheckoutCommand = `git clone --depth 1 https://github.com/aleleba/node-ts-graphql-server ${repoName}`;
const installDepsCommand = `cd ${repoName} && npm install`;
console.log(`Cloning the repository with name ${repoName}`);
const checkedOut = runCommand(gitCheckoutCommand);
if(!checkedOut) process.exit(-1);
console.log(`Installing dependencies for ${repoName}`);
const installedDeps = runCommand(installDepsCommand);
if(!installedDeps) process.exit(-1);
console.log("Congratulations! You are ready. Follow the following commands to start");
console.log(`cd ${repoName}`);
console.log('Create a .env file with ENV=development(defauld: production), PORT=4000 (default: 4000), WHITELIST_URLS=your_url(default: http://localhost), GRAPHIQL=true(default: false)');
console.log(`Then you can run: npm start:dev`);

6
jest.config.js Normal file
View File

@ -0,0 +1,6 @@
module.exports = {
testEnvironment: 'node',
transform: {
"^.+\\.ts$": "ts-jest"
},
};

7265
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -2,6 +2,7 @@
"name": "ts-graphql-server", "name": "ts-graphql-server",
"version": "0.0.1", "version": "0.0.1",
"description": "Node with Typescript and GraphQL Server", "description": "Node with Typescript and GraphQL Server",
"bin": "./bin/cli.js",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"start": "node build/index.js", "start": "node build/index.js",
@ -10,7 +11,8 @@
"build": "webpack-cli --config webpack.config.ts", "build": "webpack-cli --config webpack.config.ts",
"lint": "eslint ./ --ext .js --ext .ts", "lint": "eslint ./ --ext .js --ext .ts",
"lint:fix": "eslint ./ --ext .js --ext .ts --fix", "lint:fix": "eslint ./ --ext .js --ext .ts --fix",
"test": "echo \"Error: no test specified\" && exit 1" "test": "jest",
"test:watch": "jest --watch"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@ -60,7 +62,10 @@
"compression-webpack-plugin": "^10.0.0", "compression-webpack-plugin": "^10.0.0",
"eslint": "^8.16.0", "eslint": "^8.16.0",
"eslint-webpack-plugin": "^3.1.1", "eslint-webpack-plugin": "^3.1.1",
"jest": "^28.1.0",
"nodemon": "^2.0.16", "nodemon": "^2.0.16",
"supertest": "^6.2.3",
"ts-jest": "^28.0.3",
"ts-loader": "^9.3.0", "ts-loader": "^9.3.0",
"typescript": "^4.7.2", "typescript": "^4.7.2",
"webpack": "^5.72.1", "webpack": "^5.72.1",

View File

@ -0,0 +1,36 @@
import server from '../../index';
import supertest from 'supertest';
describe('global server tests', () => {
let request;
beforeEach(() => {
request = supertest(server);
});
test('should return Test data from test Query', async () => {
const bodyResponse = {
data: {
test: { test: 'This is the text response for Test Query from a model' }
}
}
const response = await request.get('/graphql?query=%7B%0A%20%20test%7B%0A%20%20%20%20test%0A%20%20%7D%0A%7D%0A')
.set('Accept', 'application/json')
expect(response.status).toEqual(200);
expect(response.body).toEqual(bodyResponse);
});
test('should return Test data from test Mutation', async () => {
const bodyResponse = {
data: {
testMutation: {
testMutation: 'Simulate to insert some text: test text from a model'
}
}
}
const response = await request.post('/graphql')
.send({"query":"mutation{\n testMutation{\n testMutation(text: \"test text\")\n }\n}\n","variables":null})
.set('Accept', 'application/json')
expect(response.status).toEqual(200);
expect(response.body).toEqual(bodyResponse);
});
});