assert
does not seem working as expected.
This is my package.json:
"devDependencies": {
"babel-eslint": "^7.2.3",
"backpack-core": "^0.4.1",
"mocha": "^3.5.0",
"supertest": "^3.0.0"
},
"scripts": {
"test": "NODE_PATH=./server:./server/modules mocha ./test/*.js --compilers js:babel-core/register --recursive",
"dev": "backpack dev",
"build": "nuxt build && backpack build",
"start": "cross-env NODE_ENV=production node build/main.js"
},
My test:
'use strict'
import app from '../server/index'
import supertest from 'supertest'
import assert from 'assert'
const request = supertest.agent(app)
var _id
var name
describe('POST /api/users with data', function () {
it('status code should be 200', function (done) {
request
.post('/api/users')
.type('form')
.send({name: 'tom'})
.expect('Content-Type', /json/)
.expect(200)
.end(function(err, res) {
console.log(res.body)
if (err) return done(err)
name = res.body.ops[0].name.toString()
_id = res.body.ops[0]._id.toString()
console.log(name)
assert(name, 'tomx') // should not pass this.
done()
})
})
})
Result:
POST /api/users with data
{ result: { ok: 1, n: 1 },
ops: [ { id: 'xxx', name: 'tom', _id: '59a7c33ba17bd32431c4134b' } ],
insertedCount: 1,
insertedIds: [ '59a7c33ba17bd32431c4134b' ] }
tom
✓ status code should be 200 (54ms)
At assert(name, 'tomx')
- Why is passed? It is tom
that should be passed.
Any ideas?
assert
is just a shorthand for assert.ok
which validates that the first argument is truthy https://nodejs.org/api/assert.html#assert_assert_value_message
You should use assert.equal
or assert.strictEqual
instead. Another option is modifying your assertion:
assert(name === 'tomx')