|
| 1 | +import path from 'path'; |
| 2 | +import test from 'ava'; |
| 3 | +import {RuleTester} from 'eslint'; |
| 4 | +import util from '../util'; |
| 5 | +import rule from '../rules/no-ignored-test-files'; |
| 6 | + |
| 7 | +const ruleTester = new RuleTester({ |
| 8 | + env: { |
| 9 | + es6: true |
| 10 | + } |
| 11 | +}); |
| 12 | + |
| 13 | +const header = `const test = require('ava');\n`; |
| 14 | +const rootDir = path.dirname(process.cwd()); |
| 15 | + |
| 16 | +function toPath(subPath) { |
| 17 | + return path.join(rootDir, subPath); |
| 18 | +} |
| 19 | + |
| 20 | +function code(hasHeader) { |
| 21 | + return (hasHeader ? header : '') + 'test(t => { t.pass(); });'; |
| 22 | +} |
| 23 | + |
| 24 | +test('without AVA config in package.json', () => { |
| 25 | + ruleTester.run('no-ignored-test-files', rule, { |
| 26 | + valid: [ |
| 27 | + { |
| 28 | + code: code(true), |
| 29 | + filename: toPath('test/foo/bar.js') |
| 30 | + }, |
| 31 | + { |
| 32 | + code: code(true), |
| 33 | + filename: toPath('test/foo/not-fixtures/bar.js') |
| 34 | + }, |
| 35 | + { |
| 36 | + code: code(true), |
| 37 | + filename: toPath('test/foo/not-helpers/bar.js') |
| 38 | + }, |
| 39 | + { |
| 40 | + code: header + 'foo(t => {});', |
| 41 | + filename: toPath('test/foo/fixtures/bar.js') |
| 42 | + }, |
| 43 | + { |
| 44 | + code: header + 'foo(t => {});', |
| 45 | + filename: toPath('test/foo/helpers/bar.js') |
| 46 | + }, |
| 47 | + { |
| 48 | + code: code(false), |
| 49 | + filename: toPath('test/foo/fixtures/bar.js') |
| 50 | + }, |
| 51 | + { |
| 52 | + code: code(false), |
| 53 | + filename: toPath('test/foo/helpers/bar.js') |
| 54 | + }, |
| 55 | + { |
| 56 | + code: code(true), |
| 57 | + filename: toPath('test.js') |
| 58 | + }, |
| 59 | + { |
| 60 | + code: code(true), |
| 61 | + filename: toPath('test-foo.js') |
| 62 | + }, |
| 63 | + { |
| 64 | + code: code(true), |
| 65 | + filename: toPath('lib/foo.test.js'), |
| 66 | + options: [{files: ['lib/**/*.test.js']}] |
| 67 | + } |
| 68 | + ], |
| 69 | + invalid: [ |
| 70 | + { |
| 71 | + code: code(true), |
| 72 | + filename: toPath('test/foo/fixtures/bar.js'), |
| 73 | + errors: [{message: 'Test file is ignored because it is in `**/fixtures/** **/helpers/**`'}] |
| 74 | + }, |
| 75 | + { |
| 76 | + code: code(true), |
| 77 | + filename: toPath('test/foo/helpers/bar.js'), |
| 78 | + errors: [{message: 'Test file is ignored because it is in `**/fixtures/** **/helpers/**`'}] |
| 79 | + }, |
| 80 | + { |
| 81 | + code: code(true), |
| 82 | + filename: toPath('lib/foo.test.js'), |
| 83 | + errors: [{message: 'Test file is ignored because it is not in `test.js test-*.js test/**/*.js`'}] |
| 84 | + }, |
| 85 | + { |
| 86 | + code: code(true), |
| 87 | + filename: toPath('test/foo/bar.js'), |
| 88 | + options: [{files: ['lib/**/*.test.js']}], |
| 89 | + errors: [{message: 'Test file is ignored because it is not in `lib/**/*.test.js`'}] |
| 90 | + }, |
| 91 | + { |
| 92 | + code: code(true), |
| 93 | + filename: toPath('lib/foo.not-test.js'), |
| 94 | + options: [{files: ['lib/**/*.test.js']}], |
| 95 | + errors: [{message: 'Test file is ignored because it is not in `lib/**/*.test.js`'}] |
| 96 | + } |
| 97 | + ] |
| 98 | + }); |
| 99 | +}); |
| 100 | + |
| 101 | +test('with AVA config in package.json', () => { |
| 102 | + const oldGetAvaConfig = util.getAvaConfig; |
| 103 | + |
| 104 | + util.getAvaConfig = function mockGetAvaConfig() { |
| 105 | + return { |
| 106 | + files: ['lib/**/*.test.js'] |
| 107 | + }; |
| 108 | + }; |
| 109 | + |
| 110 | + ruleTester.run('no-ignored-test-files', rule, { |
| 111 | + valid: [ |
| 112 | + { |
| 113 | + code: code(true), |
| 114 | + filename: toPath('lib/foo.test.js') |
| 115 | + }, |
| 116 | + { |
| 117 | + code: code(true), |
| 118 | + filename: toPath('bar/foo.test.js'), |
| 119 | + options: [{files: ['bar/**/*.test.js']}] |
| 120 | + } |
| 121 | + ], |
| 122 | + invalid: [ |
| 123 | + { |
| 124 | + code: code(true), |
| 125 | + filename: toPath('lib/foo/fixtures/bar.test.js'), |
| 126 | + errors: [{message: 'Test file is ignored because it is in `**/fixtures/** **/helpers/**`'}] |
| 127 | + }, |
| 128 | + { |
| 129 | + code: code(true), |
| 130 | + filename: toPath('lib/foo/helpers/bar.test.js'), |
| 131 | + errors: [{message: 'Test file is ignored because it is in `**/fixtures/** **/helpers/**`'}] |
| 132 | + }, |
| 133 | + { |
| 134 | + code: code(true), |
| 135 | + filename: toPath('test.js'), |
| 136 | + errors: [{message: 'Test file is ignored because it is not in `lib/**/*.test.js`'}] |
| 137 | + }, |
| 138 | + { |
| 139 | + code: code(true), |
| 140 | + filename: toPath('bar/foo.test.js'), |
| 141 | + errors: [{message: 'Test file is ignored because it is not in `lib/**/*.test.js`'}] |
| 142 | + }, |
| 143 | + { |
| 144 | + code: code(true), |
| 145 | + filename: toPath('lib/foo.test.js'), |
| 146 | + options: [{files: ['bar/**/*.test.js']}], |
| 147 | + errors: [{message: 'Test file is ignored because it is not in `bar/**/*.test.js`'}] |
| 148 | + } |
| 149 | + ] |
| 150 | + }); |
| 151 | + |
| 152 | + util.getAvaConfig = oldGetAvaConfig; |
| 153 | +}); |
0 commit comments