|
| 1 | +import { suite } from 'uvu'; |
| 2 | +import { readdirSync } from 'fs'; |
| 3 | +import { isAbsolute } from 'path'; |
| 4 | +import * as assert from 'uvu/assert'; |
| 5 | +import * as $ from '../src/parse'; |
| 6 | + |
| 7 | +const FILES = readdirSync(__dirname); |
| 8 | + |
| 9 | +const parse = suite('parse'); |
| 10 | + |
| 11 | +parse('should be a function', () => { |
| 12 | + assert.type($.parse, 'function'); |
| 13 | +}); |
| 14 | + |
| 15 | +parse('should rely on defaults', async () => { |
| 16 | + // dirname to avoid node_modules |
| 17 | + let output = await $.parse(__dirname); |
| 18 | + |
| 19 | + assert.type(output, 'object'); |
| 20 | + assert.is(output.dir, __dirname); |
| 21 | + |
| 22 | + assert.instance(output.suites, Array); |
| 23 | + assert.is(output.suites.length, FILES.length); |
| 24 | + |
| 25 | + output.suites.forEach(suite => { |
| 26 | + assert.is.not(isAbsolute(suite.name)); |
| 27 | + assert.is(FILES.includes(suite.name), true, '~> suite.name is relative filename') |
| 28 | + assert.is(isAbsolute(suite.file), true, '~> suite.file is absolute path'); |
| 29 | + }); |
| 30 | +}); |
| 31 | + |
| 32 | +parse.run(); |
| 33 | + |
| 34 | +// --- |
| 35 | + |
| 36 | +const dir = suite('dir'); |
| 37 | + |
| 38 | +dir('should accept relative `dir` input', async () => { |
| 39 | + let output = await $.parse('test'); |
| 40 | + |
| 41 | + assert.type(output, 'object'); |
| 42 | + assert.is(output.dir, __dirname); |
| 43 | + |
| 44 | + assert.instance(output.suites, Array); |
| 45 | + assert.is(output.suites.length, FILES.length); |
| 46 | + |
| 47 | + output.suites.forEach(suite => { |
| 48 | + assert.is.not(isAbsolute(suite.name)); |
| 49 | + assert.is(FILES.includes(suite.name), true, '~> suite.name is relative filename') |
| 50 | + assert.is(isAbsolute(suite.file), true, '~> suite.file is absolute path'); |
| 51 | + }); |
| 52 | +}); |
| 53 | + |
| 54 | +dir.run(); |
| 55 | + |
| 56 | +// --- |
| 57 | + |
| 58 | +const pattern = suite('pattern'); |
| 59 | + |
| 60 | +pattern('should only load tests matching pattern :: RegExp', async () => { |
| 61 | + let foo = await $.parse(__dirname, /assert/); |
| 62 | + assert.is(foo.suites[0].name, 'assert.js'); |
| 63 | + assert.is(foo.suites.length, 1); |
| 64 | + |
| 65 | + let bar = await $.parse(__dirname, /^uvu\.js$/); |
| 66 | + assert.is(bar.suites[0].name, 'uvu.js'); |
| 67 | + assert.is(bar.suites.length, 1); |
| 68 | +}); |
| 69 | + |
| 70 | +pattern('should only load tests matching pattern :: string', async () => { |
| 71 | + let foo = await $.parse(__dirname, 'assert'); |
| 72 | + assert.is(foo.suites[0].name, 'assert.js'); |
| 73 | + assert.is(foo.suites.length, 1); |
| 74 | + |
| 75 | + let bar = await $.parse(__dirname, '^uvu\\.js$'); |
| 76 | + assert.is(bar.suites[0].name, 'uvu.js'); |
| 77 | + assert.is(bar.suites.length, 1); |
| 78 | +}); |
| 79 | + |
| 80 | +pattern.run(); |
| 81 | + |
| 82 | +// --- |
| 83 | + |
| 84 | +const cwd = suite('options.cwd'); |
| 85 | + |
| 86 | +cwd('should affect from where `dir` resolves', async () => { |
| 87 | + let foo = await $.parse('.', '', { cwd: __dirname }); |
| 88 | + assert.is(foo.suites.length, FILES.length); |
| 89 | + foo.suites.forEach(suite => { |
| 90 | + assert.is(FILES.includes(suite.name), true, '~> suite.name is relative filename') |
| 91 | + assert.is(isAbsolute(suite.file), true, '~> suite.file is absolute path'); |
| 92 | + }); |
| 93 | +}); |
| 94 | + |
| 95 | +cwd.run(); |
| 96 | + |
| 97 | +// --- |
| 98 | + |
| 99 | +const ignore = suite('options.ignore'); |
| 100 | + |
| 101 | +ignore('should ignore test files matching :: RegExp', async () => { |
| 102 | + let foo = await $.parse(__dirname, '', { ignore: /assert/ }); |
| 103 | + assert.is(foo.suites.find(x => x.name === 'assert.js'), undefined); |
| 104 | + assert.is(foo.suites.length, FILES.length - 1); |
| 105 | + |
| 106 | + let bar = await $.parse(__dirname, '', { ignore: /^uvu\.js$/ }); |
| 107 | + assert.is(bar.suites.find(x => x.name === 'uvu.js'), undefined); |
| 108 | + assert.is(bar.suites.length, FILES.length - 1); |
| 109 | +}); |
| 110 | + |
| 111 | +ignore('should ignore test files matching :: RegExp', async () => { |
| 112 | + let foo = await $.parse(__dirname, '', { ignore: 'assert' }); |
| 113 | + assert.is(foo.suites.find(x => x.name === 'assert.js'), undefined); |
| 114 | + assert.is(foo.suites.length, FILES.length - 1); |
| 115 | + |
| 116 | + let bar = await $.parse(__dirname, '', { ignore: 'uvu.js' }); |
| 117 | + assert.is(bar.suites.find(x => x.name === 'uvu.js'), undefined); |
| 118 | + assert.is(bar.suites.length, FILES.length - 1); |
| 119 | +}); |
| 120 | + |
| 121 | +ignore.run(); |
| 122 | + |
| 123 | +// --- |
| 124 | + |
| 125 | +const requires = suite('options.require'); |
| 126 | + |
| 127 | +requires('should throw on invalid value(s)', async () => { |
| 128 | + try { |
| 129 | + await $.parse(__dirname, '', { require: ['foobar'] }); |
| 130 | + assert.unreachable('should have thrown'); |
| 131 | + } catch (err) { |
| 132 | + assert.instance(err, Error); |
| 133 | + assert.match(err.message, `Cannot find module 'foobar'`); |
| 134 | + } |
| 135 | +}); |
| 136 | + |
| 137 | +requires.run(); |
0 commit comments