Skip to content

Commit

Permalink
refactor(test): move test assets to tests dir (#1931)
Browse files Browse the repository at this point in the history
Also, rename fixture files to use consistent snake_case.
  • Loading branch information
mastermatt committed Feb 25, 2020
1 parent 9375dcd commit 2c6c9dd
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 30 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
17 changes: 8 additions & 9 deletions tests/test_back.js
Expand Up @@ -52,7 +52,7 @@ function testNock(t) {
function nockBackWithFixture(t, scopesLoaded) {
const scopesLength = scopesLoaded ? 1 : 0

nockBack('goodRequest.json', function(done) {
nockBack('good_request.json', function(done) {
t.equal(this.scopes.length, scopesLength)
http.get('http://www.example.test/')
this.assertScopesFinished()
Expand All @@ -62,7 +62,7 @@ function nockBackWithFixture(t, scopesLoaded) {
}

// TODO: This was added as a temporary patch. It's possible that we don't need
// both `goodRequest.json`/`nockBackWithFixture()` on google.com and a second
// both `good_request.json`/`nockBackWithFixture()` on google.com and a second
// pair on localhost. Consolidate them if possible. Otherwise remove this
// comment.
function nockBackWithFixtureLocalhost(t) {
Expand Down Expand Up @@ -124,7 +124,7 @@ test('nockBack returns a promise when neither options nor nockbackFn are specifi
test('nockBack throws an exception when a hook is not a function', t => {
nockBack.setMode('dryrun')
t.throws(
() => nockBack('goodRequest.json', { before: 'not-a-function-innit' }),
() => nockBack('good_request.json', { before: 'not-a-function-innit' }),
{ message: 'processing hooks must be a function' }
)
t.end()
Expand Down Expand Up @@ -341,8 +341,7 @@ test('nockBack record tests', nw => {
})

nw.test("it shouldn't allow outside calls", t => {
const fixture = 'wrongUri.json'
nockBack(fixture, function(done) {
nockBack('wrong_uri.json', function(done) {
http
.get('http://other.example.test', res =>
t.fail('Should not come here!')
Expand All @@ -359,7 +358,7 @@ test('nockBack record tests', nw => {
})

nw.test('it loads your recorded tests', t => {
nockBack('goodRequest.json', function(done) {
nockBack('good_request.json', function(done) {
t.true(this.scopes.length > 0)
http.get('http://www.example.test/').end()
this.assertScopesFinished()
Expand Down Expand Up @@ -490,7 +489,7 @@ test('nockBack lockdown tests', nw => {

test('assertScopesFinished throws exception when Back still has pending scopes', t => {
nockBack.setMode('record')
const fixtureName = 'goodRequest.json'
const fixtureName = 'good_request.json'
const fixturePath = path.join(nockBack.fixtures, fixtureName)
nockBack(fixtureName, function(done) {
const expected = `["GET http://www.example.test:80/"] was not used, consider removing ${fixturePath} to rerecord fixture`
Expand All @@ -505,7 +504,7 @@ test('nockBack dryrun throws the expected exception when fs is not available', t
nockBackWithoutFs.setMode('dryrun')

nockBackWithoutFs.fixtures = `${__dirname}/fixtures`
t.throws(() => nockBackWithoutFs('goodRequest.json'), { message: 'no fs' })
t.throws(() => nockBackWithoutFs('good_request.json'), { message: 'no fs' })

t.end()
})
Expand All @@ -515,6 +514,6 @@ test('nockBack record mode throws the expected exception when fs is not availabl
nockBackWithoutFs.setMode('record')

nockBackWithoutFs.fixtures = `${__dirname}/fixtures`
t.throws(() => nockBackWithoutFs('goodRequest.json'), { message: 'no fs' })
t.throws(() => nockBackWithoutFs('good_request.json'), { message: 'no fs' })
t.end()
})
22 changes: 11 additions & 11 deletions tests/test_delay.js
Expand Up @@ -12,7 +12,7 @@ const got = require('./got_client')

require('./cleanup_after_each')()

const textFile = path.join(__dirname, '..', 'assets', 'reply_file_1.txt')
const textFilePath = path.resolve(__dirname, './assets/reply_file_1.txt')

function checkDuration(t, ms) {
// Do not write new tests using this function. Write async tests using
Expand Down Expand Up @@ -164,14 +164,14 @@ test('delayBody works with a stream', async t => {
.get('/')
.delayBody(100)
.reply(200, (uri, requestBody) =>
fs.createReadStream(textFile, { encoding: 'utf8' })
fs.createReadStream(textFilePath, { encoding: 'utf8' })
)

await resolvesInAtLeast(
t,
async () => {
const { body } = await got('http://example.test')
t.equal(body, fs.readFileSync(textFile, { encoding: 'utf8' }))
t.equal(body, fs.readFileSync(textFilePath, { encoding: 'utf8' }))
},
100
)
Expand All @@ -185,13 +185,13 @@ test('delayBody works with a stream of binary buffers', async t => {
.delayBody(100)
// No encoding specified, which causes the file to be streamed using
// buffers instead of strings.
.reply(200, (uri, requestBody) => fs.createReadStream(textFile))
.reply(200, (uri, requestBody) => fs.createReadStream(textFilePath))

await resolvesInAtLeast(
t,
async () => {
const { body } = await got('http://example.test/')
t.equal(body, fs.readFileSync(textFile, { encoding: 'utf8' }))
t.equal(body, fs.readFileSync(textFilePath, { encoding: 'utf8' }))
},
100
)
Expand All @@ -212,10 +212,10 @@ test('delayBody works with a delayed stream', async t => {
.delayBody(100)
.reply(200, (uri, requestBody) => passthrough)

setTimeout(() => fs.createReadStream(textFile).pipe(passthrough), 125)
setTimeout(() => fs.createReadStream(textFilePath).pipe(passthrough), 125)

const { body } = await got('http://example.test/')
t.equal(body, fs.readFileSync(textFile, { encoding: 'utf8' }))
t.equal(body, fs.readFileSync(textFilePath, { encoding: 'utf8' }))

scope.done()
})
Expand Down Expand Up @@ -292,7 +292,7 @@ test('delay works with replyWithFile', t => {
nock('http://localhost')
.get('/')
.delay(100)
.replyWithFile(200, `${__dirname}/../assets/reply_file_1.txt`)
.replyWithFile(200, textFilePath)

http
.request('http://localhost/', function(res) {
Expand Down Expand Up @@ -325,7 +325,7 @@ test('delay works with when you return a generic stream from the reply callback'
.get('/')
.delay(100)
.reply(200, function(path, reqBody) {
return fs.createReadStream(`${__dirname}/../assets/reply_file_1.txt`)
return fs.createReadStream(textFilePath)
})

http
Expand Down Expand Up @@ -411,7 +411,7 @@ test('delayConnection works with replyWithFile', t => {
nock('http://localhost')
.get('/')
.delayConnection(100)
.replyWithFile(200, `${__dirname}/../assets/reply_file_1.txt`)
.replyWithFile(200, textFilePath)

http
.request('http://localhost/', function(res) {
Expand Down Expand Up @@ -444,7 +444,7 @@ test('delayConnection works with when you return a generic stream from the reply
.get('/')
.delayConnection(100)
.reply(200, function(path, reqBody) {
return fs.createReadStream(`${__dirname}/../assets/reply_file_1.txt`)
return fs.createReadStream(textFilePath)
})

http
Expand Down
4 changes: 2 additions & 2 deletions tests/test_persist_optionally.js
Expand Up @@ -12,7 +12,7 @@ const got = require('./got_client')

require('./setup')

const textFile = path.join(__dirname, '..', 'assets', 'reply_file_1.txt')
const textFilePath = path.resolve(__dirname, './assets/reply_file_1.txt')

describe('`optionally()`', () => {
it('optional mocks do not appear in `pendingMocks()`', () => {
Expand Down Expand Up @@ -191,7 +191,7 @@ describe('`persist()`', () => {
nock('http://example.test')
.persist()
.get('/')
.replyWithFile(200, textFile)
.replyWithFile(200, textFilePath)
.get('/test')
.reply(200, 'Yay!')

Expand Down
12 changes: 7 additions & 5 deletions tests/test_reply_with_file.js
Expand Up @@ -10,14 +10,14 @@ const got = require('./got_client')

require('./setup')

const textFile = path.join(__dirname, '..', 'assets', 'reply_file_1.txt')
const binaryFile = path.join(__dirname, '..', 'assets', 'reply_file_2.txt.gz')
const textFilePath = path.resolve(__dirname, './assets/reply_file_1.txt')
const binaryFilePath = path.resolve(__dirname, './assets/reply_file_2.txt.gz')

describe('`replyWithFile()`', () => {
it('reply with file', async () => {
const scope = nock('http://example.test')
.get('/')
.replyWithFile(200, textFile)
.replyWithFile(200, textFilePath)

const { statusCode, body } = await got('http://example.test/')

Expand All @@ -30,7 +30,7 @@ describe('`replyWithFile()`', () => {
it('reply with file with headers', async () => {
const scope = nock('http://example.test')
.get('/')
.replyWithFile(200, binaryFile, {
.replyWithFile(200, binaryFilePath, {
'content-encoding': 'gzip',
})

Expand All @@ -50,7 +50,9 @@ describe('`replyWithFile()`', () => {

it('throws the expected error', () => {
expect(() =>
new Scope('http://example.test').get('/').replyWithFile(200, textFile)
new Scope('http://example.test')
.get('/')
.replyWithFile(200, textFilePath)
).to.throw(Error, 'No fs')
})
})
Expand Down
4 changes: 3 additions & 1 deletion tests/test_scope.js
Expand Up @@ -12,7 +12,9 @@ const got = require('./got_client')
require('./setup')

it('scope exposes interceptors', () => {
const scopes = nock.load(path.join(__dirname, 'fixtures', 'goodRequest.json'))
const scopes = nock.load(
path.join(__dirname, 'fixtures', 'good_request.json')
)

expect(scopes).to.be.an.instanceOf(Array)
expect(scopes).to.have.lengthOf.at.least(1)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_stream.js
Expand Up @@ -12,12 +12,12 @@ const got = require('./got_client')

require('./setup')

const textFile = path.join(__dirname, '..', 'assets', 'reply_file_1.txt')
const textFilePath = path.resolve(__dirname, './assets/reply_file_1.txt')

it('reply with file and pipe response', done => {
const scope = nock('http://example.test')
.get('/')
.replyWithFile(200, textFile)
.replyWithFile(200, textFilePath)

let text = ''
const fakeStream = new stream.Stream()
Expand Down

0 comments on commit 2c6c9dd

Please sign in to comment.