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

Feature - Testing with express #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
37 changes: 0 additions & 37 deletions package-lock.json

This file was deleted.

8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "SSR Fetch for Next JS",
"main": "lib/index.js",
"scripts": {
"build": "npx tsc"
"build": "npx tsc",
"test": "jest"
},
"repository": {
"type": "git",
Expand All @@ -22,6 +23,11 @@
"author": "arn-ob",
"license": "MIT",
"devDependencies": {
"@types/jest": "^27.5.0",
"@types/mocha": "^9.1.1",
"express": "^4.18.1",
"jest": "^28.1.0",
"supertest": "^6.2.3",
"typescript": "^4.6.2"
}
}
69 changes: 69 additions & 0 deletions test/express.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const express = require('express');
const request = require("supertest");

// Create Express Application
function ExpressInit() {

let exp = express();
let router = express.Router();

router.route('/').get(function (req, res) {
return res.json({ status: true });
});

router.route('/').post(function (req, res) {
return res.json({ status: true });
});

// router
exp.use(router);

return exp;
}


describe('GET - Testing With Express', function () {
let exp;

// Called once before any of the tests in this block begin.
beforeAll(function (done) {
exp = ExpressInit();
exp.listen(function (err) {
if (err) {
console.error("Express init Error");
return done(err);
}

console.log("Express init Done");
done();
});
});

it('Return request from GET', function (done) {
request(exp)
.get('/')
.expect(200, function (err, res) {
if (err) {
console.error("Request Error");
return done(err);
}

console.log("GET - Request Body", res.body);

expect(res.statusCode).toBe(200);
done();
});
});

it('Return request from POST', function (done) {
request(exp)
.post('/')
.expect(200, function (err, res) {
if (err) { return done(err); }

console.log("POST - Request Body", res.body);
expect(res.statusCode).toBe(200);
done();
});
});
});
15 changes: 15 additions & 0 deletions test/init.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
*
* @param a number
* @param b number
* @returns sum
*/

function sum(a, b) {
return a + b;
}

test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});

Loading