Skip to content

Commit

Permalink
Adding TypeScript Module Integration Test (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkHerhold committed Jun 11, 2020
1 parent 1de29e9 commit fc01945
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 19 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -2,4 +2,5 @@
node_modules
npm-debug.log
yarn.lock
package-lock.json
package-lock.json
*.tgz
1 change: 1 addition & 0 deletions .npmrc
@@ -0,0 +1 @@
package-lock=false
14 changes: 12 additions & 2 deletions .travis.yml
@@ -1,7 +1,17 @@
language: node_js
node_js:
- "6"
- "8"
- "10"
- "12"
- "14"

jobs:
include:
# note that unit tests are run on the Node.js versions specified above

# packages koa-body and installs it in the integration project as a dependency
- stage: TS integration test
script: |
npm pack
cd test/ts-module-integration
npm install
npm run build
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
## Unreleased
- adds TypeScript integration testing
- removes testing against EOL Node.js verions

## 4.1.3
Reverted changes introduced in 4.1.2. Now 4.1.3 is effectively the same as 4.1.1.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"types": "./index.d.ts",
"scripts": {
"test": "mocha",
"test": "mocha test/unit/",
"examples-multer": "node examples/multer.js",
"examples-koa-router": "node examples/koa-router.js"
},
Expand Down
2 changes: 1 addition & 1 deletion test/mocha.opts
@@ -1,2 +1,2 @@
--reporter spec
--require should
--require should
2 changes: 0 additions & 2 deletions test/temp/.gitignore

This file was deleted.

1 change: 1 addition & 0 deletions test/ts-module-integration/.gitignore
@@ -0,0 +1 @@
dist/
2 changes: 2 additions & 0 deletions test/ts-module-integration/.npmrc
@@ -0,0 +1,2 @@
# we want to rely on koa-body's dependencies declared in package.json, not a generated lock
package-lock=false
15 changes: 15 additions & 0 deletions test/ts-module-integration/lib/index.ts
@@ -0,0 +1,15 @@
import Koa from 'koa';
import koaBody from 'koa-body';

const app = new Koa();
app.use(koaBody());

/**
* Koa-body 4.1.2 breaking changes
*/
async function m(ctx: Koa.ParameterizedContext) {
const { body } = ctx.request;
console.log(JSON.stringify(body));
}

app.use(m);
21 changes: 21 additions & 0 deletions test/ts-module-integration/package.json
@@ -0,0 +1,21 @@
{
"name": "ts-koa-body-test",
"private": true,
"version": "0.0.0",
"description": "koa-body TS integration test",
"main": "dist/lib/index.js",
"module": "dist/lib/index.js",
"types": "dist/types/index.d.ts",
"scripts": {
"build": "tsc --module commonjs"
},
"dependencies": {
"@types/koa": "^2.11.3",
"@types/node": "^11.15.10",
"koa": "^2.12.0",
"koa-body": "../../"
},
"devDependencies": {
"typescript": "^3.8.3"
}
}
35 changes: 35 additions & 0 deletions test/ts-module-integration/tsconfig.json
@@ -0,0 +1,35 @@
{
"compilerOptions": {
"target": "es6",
"module": "es2015",
"moduleResolution": "node",
"declaration": true,
"sourceMap": true,
"strict": true,
"esModuleInterop": true,
"outDir": "dist/lib",
"declarationDir": "dist/types",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"plugins": [
{
"name": "tslint-language-service"
}
],
"lib": [
"es2015",
"es2016",
"es2017"
],
"typeRoots": [
"node_modules/@types"
]
},
"include": [
"lib"
],
"exclude": [
"dist"
]
}
22 changes: 10 additions & 12 deletions test/index.js → test/unit/index.js
Expand Up @@ -12,15 +12,15 @@
const assert = require('assert');
const fs = require('fs');
const http = require('http');
const koaBody = require('../index');
const koaBody = require('../../index.js');
const path = require('path');
const request = require('supertest');
const should = require('should');
const Koa = require('koa');
const Router = require('koa-router');
const sinon = require('sinon');

const unparsed = require('../unparsed.js');
const unparsed = require('../../unparsed.js');

describe('koa-body', () => {
let database;
Expand Down Expand Up @@ -145,7 +145,7 @@ describe('koa-body', () => {
app.use(koaBody({
multipart: true,
formidable: {
uploadDir: __dirname + '/temp'
uploadDir: '/tmp'
}
}));
app.use(router.routes());
Expand All @@ -169,7 +169,7 @@ describe('koa-body', () => {
res.body.user.names[1].should.equal('Paul');
res.body._files.firstField.should.be.an.Object();
res.body._files.firstField.name.should.equal('package.json');
should(fs.statSync(res.body._files.firstField.path)).be.ok;
should(fs.statSync(res.body._files.firstField.path)).be.ok();
fs.unlinkSync(res.body._files.firstField.path);

res.body._files.secondField.should.be.an.Array().and.have.lengthOf(2);
Expand All @@ -179,8 +179,8 @@ describe('koa-body', () => {
res.body._files.secondField.should.containDeep([{
name: 'package.json'
}]);
should(fs.statSync(res.body._files.secondField[0].path)).be.ok;
should(fs.statSync(res.body._files.secondField[1].path)).be.ok;
should(fs.statSync(res.body._files.secondField[0].path)).be.ok();
should(fs.statSync(res.body._files.secondField[1].path)).be.ok();
fs.unlinkSync(res.body._files.secondField[0].path);
fs.unlinkSync(res.body._files.secondField[1].path);

Expand All @@ -195,11 +195,11 @@ describe('koa-body', () => {
res.body._files.thirdField.should.containDeep([{
name: 'package.json'
}]);
should(fs.statSync(res.body._files.thirdField[0].path)).be.ok;
should(fs.statSync(res.body._files.thirdField[0].path)).be.ok();
fs.unlinkSync(res.body._files.thirdField[0].path);
should(fs.statSync(res.body._files.thirdField[1].path)).be.ok;
should(fs.statSync(res.body._files.thirdField[1].path)).be.ok();
fs.unlinkSync(res.body._files.thirdField[1].path);
should(fs.statSync(res.body._files.thirdField[2].path)).be.ok;
should(fs.statSync(res.body._files.thirdField[2].path)).be.ok();
fs.unlinkSync(res.body._files.thirdField[2].path);

done();
Expand All @@ -210,7 +210,7 @@ describe('koa-body', () => {
app.use(koaBody({
multipart: true,
formidable: {
uploadDir: __dirname + '/temp',
uploadDir: '/tmp',
onFileBegin: (name, file) => {
file.name = 'backage.json'
const folder = path.dirname(file.path);
Expand All @@ -223,8 +223,6 @@ describe('koa-body', () => {
request(http.createServer(app.callback()))
.post('/users')
.type('multipart/form-data')
.field('names', 'John')
.field('names', 'Paul')
.attach('firstField', 'package.json')
.expect(201)
.end( (err, res) => {
Expand Down

0 comments on commit fc01945

Please sign in to comment.