javascript - Mock backend in nightwatch.js tests -
in vue.js app i'm using nightwatch test app. have following spec:
module.exports = { 'wrong email or password': function (browser) { const devserver = browser.globals.devserverurl var nock = require('nock'); var couchdb = nock('http://localhost:3000/') .get('api/v1/login') .reply(401, { error: 'dupa' }); browser .url(devserver + '/login') .setvalue('input[type=email]', 'email@example.com') .setvalue('input[type=password]', 'password') .click('.login') .assert.containstext('#app', 'niepoprawny email lub hasło.') .end() } } in test i'm trying use https://github.com/node-nock/nock. unfortunately not mocks requests. i'm doing wrong?
nock replaces http mechanism in browser environment in run. since you're running in test, isn't running in browser, browser environment unchanged.
there several things do, depends on you're trying achieve:
- you write fake server , have listen @ port 3000 , answer way like.
- you configure application use different mechanism according configuration, , have load nock 'strategy' in test.
- if have other tests checking ui, replace test unit tests , integration tests functions actual requests.
to write fake, need simple server returns answer want. here's example express.js:
const express = require('express') const app = express() app.get('/api/v1/login', function (req, res) { res.send('some response') }) app.listen(3000, function () { console.log('server listening on port 3000') })
Comments
Post a Comment