Skip to content

Commit

Permalink
Replace tape with jest
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerlong committed Sep 10, 2017
1 parent a5b7145 commit 2d91daf
Show file tree
Hide file tree
Showing 5 changed files with 927 additions and 262 deletions.
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
"upgrade": "yarn upgrade --latest && yarn remove d3 && yarn add [email protected]",
"lint": "standard",
"karma": "node -r babel-register node_modules/.bin/karma start karma.conf.js --single-run",
"tape": "node -r babel-register node_modules/.bin/tape test/cli_test-*.js",
"test": "yarn lint && yarn tape && yarn karma",
"jest": "jest --coverage",
"test": "yarn lint && yarn jest && yarn karma",
"jison": "gulp jison",
"prepublishOnly": "yarn build && yarn release && yarn test"
},
Expand Down Expand Up @@ -69,6 +69,7 @@
"inject-loader": "^3.0.1",
"jasmine": "^2.8.0",
"jasmine-es6": "^0.4.1",
"jest": "^21.0.2",
"jison": "^0.4.18",
"karma": "^1.7.1",
"karma-chrome-launcher": "^2.2.0",
Expand All @@ -80,7 +81,6 @@
"rimraf": "^2.6.1",
"standard": "^10.0.3",
"style-loader": "^0.18.2",
"tape": "^4.8.0",
"webpack": "^3.5.6",
"webpack-node-externals": "^1.6.0"
},
Expand All @@ -89,5 +89,8 @@
"dist",
"lib",
"src"
]
],
"jest": {
"testRegex": "test/cli_test-.+?\\.js"
}
}
59 changes: 32 additions & 27 deletions test/cli_test-output.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* eslint-env jest */
/* eslint-env jasmine */
const fs = require('fs')
const path = require('path')

const test = require('tape')
const async = require('async')
const clone = require('clone')
const rimraf = require('rimraf')
Expand Down Expand Up @@ -39,8 +40,12 @@ const multiFile = {
ganttConfig: null
}

test('output of single png', function (t) {
t.plan(3)
beforeEach(() => {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 64000
})

test('output of single png', function (done) {
expect.assertions(3)

const expected = ['test.mermaid.png']

Expand All @@ -49,14 +54,14 @@ test('output of single png', function (t) {
opt.png = true

mermaid.process(opt.files, opt, function (code) {
t.equal(code, 0, 'has clean exit code')
expect(code).toBe(0)

verifyFiles(expected, opt.outputDir, t)
verifyFiles(expected, opt.outputDir, done)
})
})

test('output of multiple png', function (t) {
t.plan(3)
test('output of multiple png', function (done) {
expect.assertions(3)

const expected = ['test.mermaid.png', 'test2.mermaid.png',
'gantt.mermaid.png', 'sequence.mermaid.png']
Expand All @@ -66,14 +71,14 @@ test('output of multiple png', function (t) {
opt.png = true

mermaid.process(opt.files, opt, function (code) {
t.equal(code, 0, 'has clean exit code')
expect(code).toBe(0)

verifyFiles(expected, opt.outputDir, t)
verifyFiles(expected, opt.outputDir, done)
})
})

test('output of single svg', function (t) {
t.plan(3)
test('output of single svg', function (done) {
expect.assertions(3)

const expected = ['test.mermaid.svg']

Expand All @@ -82,14 +87,14 @@ test('output of single svg', function (t) {
opt.svg = true

mermaid.process(opt.files, opt, function (code) {
t.equal(code, 0, 'has clean exit code')
expect(code).toBe(0)

verifyFiles(expected, opt.outputDir, t)
verifyFiles(expected, opt.outputDir, done)
})
})

test('output of multiple svg', function (t) {
t.plan(3)
test('output of multiple svg', function (done) {
expect.assertions(3)

const expected = ['test.mermaid.svg', 'test2.mermaid.svg',
'gantt.mermaid.svg', 'sequence.mermaid.svg']
Expand All @@ -99,14 +104,14 @@ test('output of multiple svg', function (t) {
opt.svg = true

mermaid.process(opt.files, opt, function (code) {
t.equal(code, 0, 'has clean exit code')
expect(code).toBe(0)

verifyFiles(expected, opt.outputDir, t)
verifyFiles(expected, opt.outputDir, done)
})
})

test('output including CSS', function (t) {
t.plan(5)
test('output including CSS', function (done) {
expect.assertions(5)

const expected = ['test.mermaid.png']
const opt = clone(singleFile)
Expand All @@ -118,36 +123,36 @@ test('output including CSS', function (t) {
opt2.outputDir += '_css_png'

mermaid.process(opt.files, opt, function (code) {
t.equal(code, 0, 'has clean exit code')
expect(code).toBe(0)
const filename = path.join(opt.outputDir, path.basename(expected[0]))
const one = fs.statSync(filename)

opt2.css = path.join('test', 'fixtures', 'test.css')

mermaid.process(opt2.files, opt2, function (code) {
t.equal(code, 0, 'has clean exit code')
expect(code).toBe(0)
const two = fs.statSync(filename)
t.notEqual(one.size, two.size)
expect(one.size).not.toBe(two.size)

verifyFiles(expected, opt.outputDir, t)
verifyFiles(expected, opt.outputDir, done)
})
})
})

function verifyFiles (expected, dir, t) {
function verifyFiles (expected, dir, done) {
async.each(
expected, function (file, cb) {
const filename = path.join(dir, path.basename(file))
fs.stat(filename, function (err, stat) {
cb(err)
})
}, function (err) {
t.notOk(err, 'all files passed')
expect(err).toBeFalsy()
const deleteTmps = true
const _rimraf = deleteTmps ? rimraf : function (dir, f) { f(0) }
_rimraf(dir, function (rmerr) {
t.notOk(rmerr, 'cleaned up')
t.end()
expect(rmerr).toBeFalsy()
done()
})
}
)
Expand Down
108 changes: 56 additions & 52 deletions test/cli_test-parser.js
Original file line number Diff line number Diff line change
@@ -1,131 +1,135 @@
const test = require('tape')

/* eslint-env jest */
/* eslint-env jasmine */
const cliPath = '../lib/cli'

test('parses multiple files', function (t) {
t.plan(3)
beforeEach(() => {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 64000
})

test('parses multiple files', function (done) {
expect.assertions(3)

const cli = require(cliPath)
const argv = ['example/file1.mermaid', 'file2.mermaid', 'file3.mermaid']
const expect = ['example/file1.mermaid', 'file2.mermaid', 'file3.mermaid']
const expected = ['example/file1.mermaid', 'file2.mermaid', 'file3.mermaid']

cli.parse(argv, function (err, msg, opt) {
t.ok(!err, 'no err')
t.equal(opt.files.length, 3, 'should have 3 parameters')
t.deepEqual(opt.files, expect, 'should match expected values')
expect(!err).toBeTruthy()
expect(opt.files.length).toBe(3)
expect(opt.files).toEqual(expected)

t.end()
done()
})
})

test('defaults to png', function (t) {
t.plan(3)
test('defaults to png', function (done) {
expect.assertions(3)

const cli = require(cliPath)
const argv = ['example/file1.mermaid']

cli.parse(argv, function (err, msg, opt) {
t.ok(!err, 'no err')
t.ok(opt.png, 'png is set by default')
t.notOk(opt.svg, 'svg is not set by default')
expect(!err).toBeTruthy()
expect(opt.png).toBeTruthy()
expect(opt.svg).toBeFalsy()

t.end()
done()
})
})

test('setting svg unsets png', function (t) {
t.plan(3)
test('setting svg unsets png', function (done) {
expect.assertions(3)

const cli = require(cliPath)
const argv = ['example/file1.mermaid', '-s']

cli.parse(argv, function (err, msg, opt) {
t.ok(!err, 'no err')
t.ok(opt.svg, 'svg is set when requested')
t.notOk(opt.png, 'png is unset when svg is set')
expect(!err).toBeTruthy()
expect(opt.svg).toBeTruthy()
expect(opt.png).toBeFalsy()

t.end()
done()
})
})

test('setting png and svg is allowed', function (t) {
t.plan(3)
test('setting png and svg is allowed', function (done) {
expect.assertions(3)

const cli = require(cliPath)
const argv = ['example/file1.mermaid', '-s', '-p']

cli.parse(argv, function (err, msg, opt) {
t.ok(!err, 'no err')
t.ok(opt.png, 'png is set when requested')
t.ok(opt.svg, 'svg is set when requested')
expect(!err).toBeTruthy()
expect(opt.png).toBeTruthy()
expect(opt.svg).toBeTruthy()

t.end()
done()
})
})

test('setting an output directory succeeds', function (t) {
t.plan(2)
test('setting an output directory succeeds', function (done) {
expect.assertions(2)

const cli = require(cliPath)
const argv = ['example/file1.mermaid', '-o', 'example/']

cli.parse(argv, function (err, msg, opt) {
t.ok(!err, 'no err')
t.equal(opt.outputDir, 'example/', 'output directory is set')
t.end()
expect(!err).toBeTruthy()
expect(opt.outputDir).toBe('example/')
done()
})
})

test('not setting a css source file uses a default style', function (t) {
t.plan(2)
test('not setting a css source file uses a default style', function (done) {
expect.assertions(2)

const cli = require(cliPath)
const argv = ['example/file1.mermaid']

cli.parse(argv, function (err, msg, opt) {
t.ok(!err, 'no err')
t.ok(opt.css, 'css file is populated')
t.end()
expect(!err).toBeTruthy()
expect(opt.css).toBeTruthy()
done()
})
})

test('setting a css source file succeeds', function (t) {
t.plan(2)
test('setting a css source file succeeds', function (done) {
expect.assertions(2)

const cli = require(cliPath)
const argv = ['example/file1.mermaid', '-t', 'test/fixtures/test.css']

cli.parse(argv, function (err, msg, opt) {
t.ok(!err, 'no err')
t.ok(opt.css, 'css file is populated')
t.end()
expect(!err).toBeTruthy()
expect(opt.css).toBeTruthy()
done()
})
})

test('setting an output directory incorrectly causes an error', function (t) {
t.plan(1)
test('setting an output directory incorrectly causes an error', function (done) {
expect.assertions(1)

const cli = require(cliPath)
const argv = ['-o']

cli.parse(argv, function (err) {
t.ok(err, 'an error is raised')
expect(err).toBeTruthy()

t.end()
done()
})
})

test('a callback function is called after parsing', function (t) {
t.plan(3)
test('a callback function is called after parsing', function (done) {
expect.assertions(3)

const cli = require(cliPath)
const argv = ['example/file1.mermaid']

cli.parse(argv, function (err, msg, opts) {
t.ok(!err, 'no err')
t.ok(true, 'callback was called')
t.deepEqual(argv, opts.files, 'options are as expected')
expect(!err).toBeTruthy()
expect(true).toBeTruthy()
expect(argv).toEqual(opts.files)

t.end()
done()
})
})
Loading

0 comments on commit 2d91daf

Please sign in to comment.