chore(TypeScript): fix unit tests and coverage

This commit is contained in:
harttle
2019-02-17 20:46:33 +08:00
parent fc9ebdb90f
commit aa49b4f117
8 changed files with 43 additions and 19 deletions
+1 -1
View File
@@ -8,7 +8,7 @@ jobs:
name: 'Linting' name: 'Linting'
script: npm run lint script: npm run lint
- name: 'Coverage' - name: 'Coverage'
script: npm run coverage && npm run coveralls script: npm run coverage-coveralls
- stage: release - stage: release
if: branch = master if: branch = master
script: skip script: skip
+8 -4
View File
@@ -7,11 +7,11 @@
"browser": "dist/liquid.js", "browser": "dist/liquid.js",
"scripts": { "scripts": {
"lint": "eslint . --ext .ts --ext .js", "lint": "eslint . --ext .ts --ext .js",
"unit": "mocha -r ts-node/register -r tsconfig-paths/register test/unit/**.ts", "unit": "mocha test/unit/*.ts test/unit/**/*.ts",
"e2e": "npm run build && mocha -r ts-node/register -r tsconfig-paths/register test/e2e/**/*.ts", "e2e": "npm run build && mocha test/e2e/**/*.ts test/e2e/*.ts",
"test": "npm run unit && npm run e2e", "test": "npm run unit && npm run e2e",
"coverage": "nyc --reporter=html npm run unit", "coverage-html": "nyc npm run unit && nyc report --reporter=html",
"coveralls": "nyc report --reporter=text-lcov | coveralls", "coverage-coveralls": "nyc npm run unit && nyc report --reporter=text-lcov | coveralls",
"build": "rollup -c && ls -lh dist", "build": "rollup -c && ls -lh dist",
"version": "npm run build && git add -A dist" "version": "npm run build && git add -A dist"
}, },
@@ -68,6 +68,7 @@
"semantic-release": "^15.13.3", "semantic-release": "^15.13.3",
"sinon": "^7.2.3", "sinon": "^7.2.3",
"sinon-chai": "^3.3.0", "sinon-chai": "^3.3.0",
"source-map-support": "^0.5.10",
"supertest": "^3.4.2", "supertest": "^3.4.2",
"ts-node": "^8.0.2", "ts-node": "^8.0.2",
"tsconfig-paths": "^3.8.0", "tsconfig-paths": "^3.8.0",
@@ -82,5 +83,8 @@
"@semantic-release/npm", "@semantic-release/npm",
"@semantic-release/git" "@semantic-release/git"
] ]
},
"nyc": {
"extension": [ ".ts" ]
} }
} }
+7 -5
View File
@@ -1,10 +1,12 @@
import * as _ from '../util/underscore' import * as _ from '../util/underscore'
import * as path from 'path' import * as path from 'path'
import { anySeries } from '../util/promise' import { anySeries } from '../util/promise'
import * as fs from 'fs' import { stat, readFile } from 'fs'
const statFileAsync = <(filepath: string) => Promise<object>>_.promisify(fs.stat) export const fs = {
const readFileAsync = <(filepath: string, encoding: string) => Promise<string>>_.promisify(fs.readFile) stat: _.promisify(stat) as ((filepath: string) => Promise<object>),
readFile: _.promisify(readFile) as ((filepath: string, encoding: string) => Promise<string>)
}
export async function resolve (filepath, root, options) { export async function resolve (filepath, root, options) {
if (!path.extname(filepath)) { if (!path.extname(filepath)) {
@@ -15,7 +17,7 @@ export async function resolve (filepath, root, options) {
const paths = root.map(root => path.resolve(root, filepath)) const paths = root.map(root => path.resolve(root, filepath))
return anySeries(paths, async path => { return anySeries(paths, async path => {
try { try {
await statFileAsync(path) await fs.stat(path)
return path return path
} catch (e) { } catch (e) {
e.message = `${e.code}: Failed to lookup ${filepath} in: ${root}` e.message = `${e.code}: Failed to lookup ${filepath} in: ${root}`
@@ -25,5 +27,5 @@ export async function resolve (filepath, root, options) {
} }
export async function read (filepath): Promise<string> { export async function read (filepath): Promise<string> {
return readFileAsync(filepath, 'utf8') return fs.readFile(filepath, 'utf8')
} }
+2
View File
@@ -0,0 +1,2 @@
--require ts-node/register
--require tsconfig-paths/register
+1 -1
View File
@@ -12,7 +12,7 @@ describe('LiquidOptions#trimming', function () {
}) })
it('should respect trim_tag_right', async function () { it('should respect trim_tag_right', async function () {
const engine = new Liquid({ trim_tag_right: true }) const engine = new Liquid({ trim_tag_right: true })
const html = engine.parseAndRender('\t{%if true%}foo{%endif%} \n') const html = await engine.parseAndRender('\t{%if true%}foo{%endif%} \n')
return expect(html).to.equal('\tfoo') return expect(html).to.equal('\tfoo')
}) })
it('should not trim value', async function () { it('should not trim value', async function () {
+19 -4
View File
@@ -1,13 +1,28 @@
import { resolve } from '../../src/parser/template' import { resolve } from '../../src/parser/template'
import * as mock from 'mock-fs'
import { expect } from 'chai'
import * as path from 'path' import * as path from 'path'
import { fs } from 'src/parser/template'
import { expect, use } from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
use(chaiAsPromised)
describe('template', function () { describe('template', function () {
let readFile, stat
before(function () { before(function () {
mock({ readFile = fs.readFile
'/foo/bar.html': 'bar' stat = fs.stat
fs.readFile = async function (file) {
if (file === '/foo/bar.html') return 'bar'
throw new Error('NOENT')
}
fs.stat = async function (file) {
if (file === '/foo/bar.html') return { type: 'file' }
throw new Error('NOENT')
}
}) })
after(function () {
fs.readFile = readFile
fs.stat = stat
}) })
describe('#resolve()', function () { describe('#resolve()', function () {
it('should resolve based on root', async function () { it('should resolve based on root', async function () {
+2 -1
View File
@@ -47,7 +47,8 @@ describe('error', function () {
it('should contain stack in err.stack', async function () { it('should contain stack in err.stack', async function () {
const err = await expect(engine.parseAndRender('{% . a %}')).be.rejected const err = await expect(engine.parseAndRender('{% . a %}')).be.rejected
expect(err.message).to.contain('illegal tag syntax') expect(err.message).to.contain('illegal tag syntax')
expect(err.stack).to.contain('at Object.parse') console.log(err.stack)
expect(err.stack).to.contain('at Liquid.parse')
}) })
describe('captureStackTrace compatibility', function () { describe('captureStackTrace compatibility', function () {
const captureStackTrace = Error.captureStackTrace const captureStackTrace = Error.captureStackTrace
+2 -2
View File
@@ -52,9 +52,9 @@ describe('util/promise', function () {
.then(() => expect(spy).to.not.have.been.called) .then(() => expect(spy).to.not.have.been.called)
}) })
}) })
describe('.mapSeries()', function () { describe('.mapSeries()', async function () {
it('should resolve when all resolved', async function () { it('should resolve when all resolved', async function () {
const result = P.mapSeries( const result = await P.mapSeries(
['first', 'second', 'third'], ['first', 'second', 'third'],
item => Promise.resolve(item) item => Promise.resolve(item)
) )