ava-files.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. 'use strict';
  2. const path = require('path');
  3. const tap = require('tap');
  4. const AvaFiles = require('../lib/ava-files');
  5. const test = tap.test;
  6. tap.afterEach(done => {
  7. // We changed the CWD in some of the tests
  8. process.chdir(path.resolve(__dirname, '..'));
  9. done();
  10. });
  11. function fixture() {
  12. const args = Array.prototype.slice.call(arguments);
  13. args.unshift(__dirname, 'fixture', 'ava-files');
  14. return path.join.apply(path, args);
  15. }
  16. test('ignores relativeness in patterns', t => {
  17. const avaFiles = new AvaFiles({files: ['./foo']});
  18. const file = avaFiles.files[0];
  19. t.is(file, 'foo');
  20. t.end();
  21. });
  22. test('testMatcher', t => {
  23. const avaFiles = new AvaFiles({files: ['**/foo*']});
  24. function isTest(file) {
  25. t.true(avaFiles.isTest(file), `${file} should be a test`);
  26. }
  27. function notTest(file) {
  28. t.false(avaFiles.isTest(file), `${file} should not be a test`);
  29. }
  30. isTest('foo-bar.js');
  31. isTest('foo.js');
  32. isTest('foo/blah.js');
  33. isTest('bar/foo.js');
  34. isTest('bar/foo-bar/baz/buz.js');
  35. notTest('bar/baz/buz.js');
  36. notTest('bar.js');
  37. notTest('bar/bar.js');
  38. notTest('_foo-bar.js');
  39. notTest('foo/_foo-bar.js');
  40. notTest('foo-bar.txt');
  41. notTest('node_modules/foo.js');
  42. notTest('fixtures/foo.js');
  43. notTest('helpers/foo.js');
  44. t.end();
  45. });
  46. test('sourceMatcher - defaults', t => {
  47. const avaFiles = new AvaFiles({files: ['**/foo*']});
  48. function isSource(file) {
  49. t.true(avaFiles.isSource(file), `${file} should be a source`);
  50. }
  51. function notSource(file) {
  52. t.false(avaFiles.isSource(file), `${file} should not be a source`);
  53. }
  54. isSource('foo-bar.js');
  55. isSource('foo.js');
  56. isSource('foo/blah.js');
  57. isSource('bar/foo.js');
  58. isSource('_foo-bar.js');
  59. isSource('foo/_foo-bar.js');
  60. isSource('fixtures/foo.js');
  61. isSource('helpers/foo.js');
  62. // TODO: Watcher should probably track any required file that matches the source pattern and has a require extension installed for the given extension.
  63. notSource('foo-bar.json');
  64. notSource('foo-bar.coffee');
  65. // These seem OK
  66. isSource('bar.js');
  67. isSource('bar/bar.js');
  68. notSource('node_modules/foo.js');
  69. t.end();
  70. });
  71. test('sourceMatcher - allow matching specific node_modules directories', t => {
  72. const avaFiles = new AvaFiles({
  73. files: ['**/foo*'],
  74. sources: ['node_modules/foo/**']
  75. });
  76. t.true(avaFiles.isSource('node_modules/foo/foo.js'));
  77. t.false(avaFiles.isSource('node_modules/bar/foo.js'));
  78. t.end();
  79. });
  80. test('sourceMatcher - providing negation patterns', t => {
  81. const avaFiles = new AvaFiles({
  82. files: ['**/foo*'],
  83. sources: ['!**/bar*']
  84. });
  85. t.false(avaFiles.isSource('node_modules/foo/foo.js'));
  86. t.false(avaFiles.isSource('bar.js'));
  87. t.false(avaFiles.isSource('foo/bar.js'));
  88. t.end();
  89. });
  90. test('findFiles - does not return duplicates of the same file', t => {
  91. const avaFiles = new AvaFiles({files: ['**/ava-files/no-duplicates/**']});
  92. avaFiles.findTestFiles().then(files => {
  93. t.is(files.length, 2);
  94. t.end();
  95. });
  96. });
  97. test('findFiles - honors cwd option', t => {
  98. const avaFiles = new AvaFiles({
  99. files: ['**/test/*.js'],
  100. cwd: fixture('cwd', 'dir-b')
  101. });
  102. avaFiles.findTestFiles().then(files => {
  103. t.is(files.length, 1);
  104. t.is(path.basename(files[0]), 'baz.js');
  105. t.end();
  106. });
  107. });
  108. test('findFiles - finds the correct files by default', t => {
  109. const fixtureDir = fixture('default-patterns');
  110. process.chdir(fixtureDir);
  111. const expected = [
  112. 'sub/directory/__tests__/foo.js',
  113. 'sub/directory/bar.test.js',
  114. 'test-foo.js',
  115. 'test.js',
  116. 'test/baz.js',
  117. 'test/deep/deep.js'
  118. ].map(file => path.join(fixtureDir, file)).sort();
  119. const avaFiles = new AvaFiles();
  120. avaFiles.findTestFiles().then(files => {
  121. files.sort();
  122. t.deepEqual(files, expected);
  123. t.end();
  124. });
  125. });
  126. test('findFiles - finds the files with configured extensions (single)', t => {
  127. const fixtureDir = fixture('custom-extension');
  128. process.chdir(fixtureDir);
  129. const expected = [
  130. 'test/foo.jsx',
  131. 'test/sub/bar.jsx'
  132. ].sort().map(file => path.join(fixtureDir, file));
  133. const avaFiles = new AvaFiles({
  134. extensions: ['jsx']
  135. });
  136. avaFiles.findTestFiles().then(files => {
  137. t.deepEqual(files.sort(), expected);
  138. t.end();
  139. });
  140. });
  141. test('findFiles - finds the files with configured extensions (multiple)', t => {
  142. const fixtureDir = fixture('custom-extension');
  143. process.chdir(fixtureDir);
  144. const expected = [
  145. 'test/do-not-compile.js',
  146. 'test/foo.jsx',
  147. 'test/sub/bar.jsx'
  148. ].sort().map(file => path.join(fixtureDir, file));
  149. const avaFiles = new AvaFiles({
  150. extensions: ['jsx', 'js']
  151. });
  152. avaFiles.findTestFiles().then(files => {
  153. t.deepEqual(files.sort(), expected);
  154. t.end();
  155. });
  156. });
  157. test('findTestHelpers - finds the test helpers', t => {
  158. const fixtureDir = fixture('default-patterns');
  159. process.chdir(fixtureDir);
  160. const expected = [
  161. 'sub/directory/__tests__/helpers/foo.js',
  162. 'sub/directory/__tests__/_foo.js',
  163. 'test/helpers/test.js',
  164. 'test/_foo-help.js'
  165. ].sort().map(file => path.join(fixtureDir, file));
  166. const avaFiles = new AvaFiles();
  167. avaFiles.findTestHelpers().then(files => {
  168. t.deepEqual(files.sort(), expected);
  169. t.end();
  170. });
  171. });
  172. test('findFiles - finds the test helpers with configured extensions (single)', t => {
  173. const fixtureDir = fixture('custom-extension');
  174. process.chdir(fixtureDir);
  175. const expected = [
  176. 'test/sub/_helper.jsx',
  177. 'test/helpers/a.jsx'
  178. ].sort().map(file => path.join(fixtureDir, file));
  179. const avaFiles = new AvaFiles({
  180. extensions: ['jsx']
  181. });
  182. avaFiles.findTestHelpers().then(files => {
  183. t.deepEqual(files.sort(), expected);
  184. t.end();
  185. });
  186. });
  187. test('findFiles - finds the test helpers with configured extensions (multiple)', t => {
  188. const fixtureDir = fixture('custom-extension');
  189. process.chdir(fixtureDir);
  190. const expected = [
  191. 'test/sub/_helper.jsx',
  192. 'test/helpers/a.jsx',
  193. 'test/helpers/b.js'
  194. ].sort().map(file => path.join(fixtureDir, file));
  195. const avaFiles = new AvaFiles({
  196. extensions: ['jsx', 'js']
  197. });
  198. avaFiles.findTestHelpers().then(files => {
  199. t.deepEqual(files.sort(), expected);
  200. t.end();
  201. });
  202. });