diff --git a/src/batch.ts b/src/batch.ts index 44c9b9a..c9ddde6 100644 --- a/src/batch.ts +++ b/src/batch.ts @@ -164,7 +164,7 @@ export default class Batch { function next(): void { const latestFn = batch.shift(); if (!latestFn) return; - if (latestFn.length) { + if (latestFn.length && typeof latestFn === 'function') { latestFn(next); return; } diff --git a/src/expectations.ts b/src/expectations.ts index fe64af2..8ff2d67 100644 --- a/src/expectations.ts +++ b/src/expectations.ts @@ -40,10 +40,12 @@ function error( if (actual) err.actual = actual; // Use expect to show diffs between actual and expected values - try { - expect(actual).toEqual(expected); - } catch (e) { - console.log(e.message); + if (typeof expected === 'string') { + try { + expect(actual).toEqual(expected); + } catch (e) { + console.log(e.message); + } } // Show the stdout if the command errored diff --git a/src/runner.ts b/src/runner.ts index 2011a6d..7cb958c 100644 --- a/src/runner.ts +++ b/src/runner.ts @@ -1,3 +1,4 @@ +import fs from 'fs'; import clone from 'clone'; import { spawn } from 'child_process'; import AssertionError from 'assertion-error'; @@ -210,8 +211,13 @@ export default class Runner { * @returns instance for chaining */ - public cwd(path: string): Runner { - this.environment.cwd = path; + public cwd(cwdPath: string): Runner { + this.batch.add(() => { + if (!fs.existsSync(cwdPath)) { + throw new Error(`The path "${cwdPath}" does not exist`); + } + this.environment.cwd = cwdPath; + }); return this; } diff --git a/tests/cwd.test.ts b/tests/cwd.test.ts new file mode 100644 index 0000000..7719709 --- /dev/null +++ b/tests/cwd.test.ts @@ -0,0 +1,45 @@ +import path from 'path'; +import fs from 'fs'; +import os from 'os'; +import { Joker } from '.'; + +function mkdirTmpDir() { + const tmpDir = path.join( + os.tmpdir(), + String(Math.floor(Math.random() * 100)) + ); + if (fs.existsSync(tmpDir)) { + fs.rmdirSync(tmpDir); + } + return tmpDir; +} + +describe('Joker#cwd', () => { + it('should accept given path', async () => { + const tmpDir = mkdirTmpDir(); + await new Joker() + .cwd(tmpDir) + .run('echo foo') + .end(); + }); + + it('should change allow changing cwd to created dir', async () => { + const tmpDir = mkdirTmpDir(); + await new Joker() + .mkdir(tmpDir) + .cwd(tmpDir) + .run('echo foo') + .rmdir(tmpDir) + .end(); + expect(fs.existsSync(tmpDir)).toBe(false); + }); + + it('should fail if given path does not exist', async () => { + await expect( + new Joker() + .cwd('non_existent_path') + .run('echo foo') + .end() + ).rejects.toThrow('The path "non_existent_path" does not exist'); + }); +});