Best JavaScript code snippet using jest
58watchman.test.js
Source:58watchman.test.js
...100 afterEach(() => {101 watchman.Client.mock.instances[0].command.mockClear();102 });103 test('returns a list of all files when there are no clocks', () =>104 watchmanCrawl({105 data: {106 clocks: new Map(),107 files: new Map(),108 },109 extensions: ['js', 'json'],110 ignore: pearMatcher,111 rootDir: ROOT_MOCK,112 roots: ROOTS,113 }).then(data => {114 const client = watchman.Client.mock.instances[0];115 const calls = client.command.mock.calls;116 expect(client.on).toBeCalled();117 expect(client.on).toBeCalledWith('error', expect.any(Function));118 // Call 0 and 1 are for ['watch-project']119 expect(calls[0][0][0]).toEqual('watch-project');120 expect(calls[1][0][0]).toEqual('watch-project');121 // Call 2 is the query122 const query = calls[2][0];123 expect(query[0]).toEqual('query');124 expect(query[2].expression).toEqual([125 'allof',126 ['type', 'f'],127 ['anyof', ['suffix', 'js'], ['suffix', 'json']],128 ['anyof', ['dirname', 'fruits'], ['dirname', 'vegetables']],129 ]);130 expect(query[2].fields).toEqual(['name', 'exists', 'mtime_ms']);131 expect(query[2].glob).toEqual([132 'fruits/**/*.js',133 'fruits/**/*.json',134 'vegetables/**/*.js',135 'vegetables/**/*.json',136 ]);137 expect(data.clocks).toEqual(138 createMap({139 '': 'c:fake-clock:1',140 }),141 );142 expect(data.files).toEqual(mockFiles);143 expect(client.end).toBeCalled();144 }));145 test('applies the mapper when needed', () => {146 mockResponse = {147 'list-capabilities': {148 [undefined]: {149 capabilities: ['field-content.sha1hex'],150 },151 },152 query: {153 [ROOT_MOCK]: {154 clock: 'c:fake-clock:1',155 files: [156 {157 exists: true,158 mtime_ms: {toNumber: () => 33},159 name: 'vegetables/durian.zip',160 },161 ],162 is_fresh_instance: true,163 version: '4.5.0',164 },165 },166 'watch-project': WATCH_PROJECT_MOCK,167 };168 return watchmanCrawl({169 data: {170 clocks: new Map(),171 files: new Map(),172 },173 extensions: ['js', 'json', 'zip'],174 ignore: pearMatcher,175 mapper: n =>176 n.endsWith('.zip')177 ? [path.join(n, 'foo.1.js'), path.join(n, 'foo.2.js')]178 : null,179 rootDir: ROOT_MOCK,180 roots: ROOTS,181 }).then(data => {182 expect(data.files).toEqual(183 createMap({184 [path.join(DURIAN_RELATIVE, 'foo.1.js')]: ['', 33, 0, [], null],185 [path.join(DURIAN_RELATIVE, 'foo.2.js')]: ['', 33, 0, [], null],186 }),187 );188 });189 });190 test('updates the file object when the clock is given', () => {191 mockResponse = {192 'list-capabilities': {193 [undefined]: {194 capabilities: ['field-content.sha1hex'],195 },196 },197 query: {198 [ROOT_MOCK]: {199 clock: 'c:fake-clock:2',200 files: [201 {202 exists: true,203 mtime_ms: {toNumber: () => 42},204 name: 'fruits/kiwi.js',205 },206 {207 exists: false,208 mtime_ms: null,209 name: 'fruits/tomato.js',210 },211 ],212 is_fresh_instance: false,213 version: '4.5.0',214 },215 },216 'watch-project': WATCH_PROJECT_MOCK,217 };218 const clocks = createMap({219 '': 'c:fake-clock:1',220 });221 return watchmanCrawl({222 data: {223 clocks,224 files: mockFiles,225 },226 extensions: ['js', 'json'],227 ignore: pearMatcher,228 rootDir: ROOT_MOCK,229 roots: ROOTS,230 }).then(data => {231 // The object was reused.232 expect(data.files).toBe(mockFiles);233 expect(data.clocks).toEqual(234 createMap({235 '': 'c:fake-clock:2',236 }),237 );238 expect(data.files).toEqual(239 createMap({240 [KIWI_RELATIVE]: ['', 42, 0, [], null],241 [MELON_RELATIVE]: ['', 33, 0, [], null],242 [STRAWBERRY_RELATIVE]: ['', 30, 0, [], null],243 }),244 );245 });246 });247 test('resets the file object when watchman is restarted', () => {248 const mockTomatoSha1 = '321f6b7e8bf7f29aab89c5e41a555b1b0baa41a9';249 mockResponse = {250 'list-capabilities': {251 [undefined]: {252 capabilities: ['field-content.sha1hex'],253 },254 },255 query: {256 [ROOT_MOCK]: {257 clock: 'c:fake-clock:3',258 files: [259 {260 exists: true,261 mtime_ms: {toNumber: () => 42},262 name: 'fruits/kiwi.js',263 },264 {265 exists: true,266 mtime_ms: {toNumber: () => 41},267 name: 'fruits/banana.js',268 },269 {270 'content.sha1hex': mockTomatoSha1,271 exists: true,272 mtime_ms: {toNumber: () => 76},273 name: 'fruits/tomato.js',274 },275 ],276 is_fresh_instance: true,277 version: '4.5.0',278 },279 },280 'watch-project': WATCH_PROJECT_MOCK,281 };282 const mockBananaMetadata = ['Banana', 41, 1, ['Raspberry'], null];283 mockFiles.set(BANANA_RELATIVE, mockBananaMetadata);284 const mockTomatoMetadata = ['Tomato', 31, 1, [], mockTomatoSha1];285 mockFiles.set(TOMATO_RELATIVE, mockTomatoMetadata);286 const clocks = createMap({287 '': 'c:fake-clock:1',288 });289 return watchmanCrawl({290 data: {291 clocks,292 files: mockFiles,293 },294 extensions: ['js', 'json'],295 ignore: pearMatcher,296 rootDir: ROOT_MOCK,297 roots: ROOTS,298 }).then(data => {299 // The file object was *not* reused.300 expect(data.files).not.toBe(mockFiles);301 expect(data.clocks).toEqual(302 createMap({303 '': 'c:fake-clock:3',304 }),305 );306 // /fruits/strawberry.js was removed from the file list.307 expect(data.files).toEqual(308 createMap({309 [BANANA_RELATIVE]: mockBananaMetadata,310 [KIWI_RELATIVE]: ['', 42, 0, [], null],311 [TOMATO_RELATIVE]: ['Tomato', 76, 1, [], mockTomatoSha1],312 }),313 );314 // Even though the file list was reset, old file objects are still reused315 // if no changes have been made316 expect(data.files.get(BANANA_RELATIVE)).toBe(mockBananaMetadata);317 // Old file objects are not reused if they have a different mtime318 expect(data.files.get(TOMATO_RELATIVE)).not.toBe(mockTomatoMetadata);319 });320 });321 test('properly resets the file map when only one watcher is reset', () => {322 mockResponse = {323 'list-capabilities': {324 [undefined]: {325 capabilities: ['field-content.sha1hex'],326 },327 },328 query: {329 [FRUITS]: {330 clock: 'c:fake-clock:3',331 files: [332 {333 exists: true,334 mtime_ms: {toNumber: () => 42},335 name: 'kiwi.js',336 },337 ],338 is_fresh_instance: false,339 version: '4.5.0',340 },341 [VEGETABLES]: {342 clock: 'c:fake-clock:4',343 files: [344 {345 exists: true,346 mtime_ms: {toNumber: () => 33},347 name: 'melon.json',348 },349 ],350 is_fresh_instance: true,351 version: '4.5.0',352 },353 },354 'watch-project': {355 [FRUITS]: {356 watch: forcePOSIXPaths(FRUITS),357 },358 [VEGETABLES]: {359 watch: forcePOSIXPaths(VEGETABLES),360 },361 },362 };363 const clocks = createMap({364 [FRUITS_RELATIVE]: 'c:fake-clock:1',365 [VEGETABLES_RELATIVE]: 'c:fake-clock:2',366 });367 return watchmanCrawl({368 data: {369 clocks,370 files: mockFiles,371 },372 extensions: ['js', 'json'],373 ignore: pearMatcher,374 rootDir: ROOT_MOCK,375 roots: ROOTS,376 }).then(data => {377 expect(data.clocks).toEqual(378 createMap({379 [FRUITS_RELATIVE]: 'c:fake-clock:3',380 [VEGETABLES_RELATIVE]: 'c:fake-clock:4',381 }),382 );383 expect(data.files).toEqual(384 createMap({385 [KIWI_RELATIVE]: ['', 42, 0, [], null],386 [MELON_RELATIVE]: ['', 33, 0, [], null],387 }),388 );389 });390 });391 test('does not add directory filters to query when watching a ROOT', () => {392 mockResponse = {393 'list-capabilities': {394 [undefined]: {395 capabilities: ['field-content.sha1hex'],396 },397 },398 query: {399 [ROOT_MOCK]: {400 clock: 'c:fake-clock:1',401 files: [],402 is_fresh_instance: false,403 version: '4.5.0',404 },405 },406 'watch-project': {407 [FRUITS]: {408 relative_path: 'fruits',409 watch: forcePOSIXPaths(ROOT_MOCK),410 },411 [ROOT_MOCK]: {412 watch: forcePOSIXPaths(ROOT_MOCK),413 },414 [VEGETABLES]: {415 relative_path: 'vegetables',416 watch: forcePOSIXPaths(ROOT_MOCK),417 },418 },419 };420 return watchmanCrawl({421 data: {422 clocks: new Map(),423 files: new Map(),424 },425 extensions: ['js', 'json'],426 ignore: pearMatcher,427 rootDir: ROOT_MOCK,428 roots: [...ROOTS, ROOT_MOCK],429 }).then(data => {430 const client = watchman.Client.mock.instances[0];431 const calls = client.command.mock.calls;432 expect(client.on).toBeCalled();433 expect(client.on).toBeCalledWith('error', expect.any(Function));434 // First 3 calls are for ['watch-project']435 expect(calls[0][0][0]).toEqual('watch-project');436 expect(calls[1][0][0]).toEqual('watch-project');437 expect(calls[2][0][0]).toEqual('watch-project');438 // Call 4 is the query439 const query = calls[3][0];440 expect(query[0]).toEqual('query');441 expect(query[2].expression).toEqual([442 'allof',443 ['type', 'f'],444 ['anyof', ['suffix', 'js'], ['suffix', 'json']],445 ]);446 expect(query[2].fields).toEqual(['name', 'exists', 'mtime_ms']);447 expect(query[2].glob).toEqual(['**/*.js', '**/*.json']);448 expect(data.clocks).toEqual(449 createMap({450 '': 'c:fake-clock:1',451 }),452 );453 expect(data.files).toEqual(createMap({}));454 expect(client.end).toBeCalled();455 });456 });457 test('SHA-1 requested and available', async () => {458 mockResponse = {459 'list-capabilities': {460 [undefined]: {461 capabilities: ['field-content.sha1hex'],462 },463 },464 query: {465 [ROOT_MOCK]: {466 clock: 'c:fake-clock:1',467 files: [],468 is_fresh_instance: false,469 version: '4.5.0',470 },471 },472 'watch-project': {473 [ROOT_MOCK]: {474 watch: forcePOSIXPaths(ROOT_MOCK),475 },476 },477 };478 await watchmanCrawl({479 computeSha1: true,480 data: {481 clocks: new Map(),482 files: new Map(),483 },484 extensions: ['js', 'json'],485 rootDir: ROOT_MOCK,486 roots: [ROOT_MOCK],487 });488 const client = watchman.Client.mock.instances[0];489 const calls = client.command.mock.calls;490 expect(calls[0][0]).toEqual(['list-capabilities']);491 expect(calls[2][0][2].fields).toContain('content.sha1hex');492 });493 test('SHA-1 requested and NOT available', async () => {494 mockResponse = {495 'list-capabilities': {496 [undefined]: {497 capabilities: [],498 },499 },500 query: {501 [ROOT_MOCK]: {502 clock: 'c:fake-clock:1',503 files: [],504 is_fresh_instance: false,505 version: '4.5.0',506 },507 },508 'watch-project': {509 [ROOT_MOCK]: {510 watch: forcePOSIXPaths(ROOT_MOCK),511 },512 },513 };514 await watchmanCrawl({515 computeSha1: true,516 data: {517 clocks: new Map(),518 files: new Map(),519 },520 extensions: ['js', 'json'],521 rootDir: ROOT_MOCK,522 roots: [ROOT_MOCK],523 });524 const client = watchman.Client.mock.instances[0];525 const calls = client.command.mock.calls;526 expect(calls[0][0]).toEqual(['list-capabilities']);527 expect(calls[2][0][2].fields).not.toContain('content.sha1hex');528 });...
watchman.test.js
Source:watchman.test.js
...100 afterEach(() => {101 watchman.Client.mock.instances[0].command.mockClear();102 });103 test('returns a list of all files when there are no clocks', () =>104 watchmanCrawl({105 data: {106 clocks: new Map(),107 files: new Map(),108 },109 extensions: ['js', 'json'],110 ignore: pearMatcher,111 rootDir: ROOT_MOCK,112 roots: ROOTS,113 }).then(data => {114 const client = watchman.Client.mock.instances[0];115 const calls = client.command.mock.calls;116 expect(client.on).toBeCalled();117 expect(client.on).toBeCalledWith('error', expect.any(Function));118 // Call 0 and 1 are for ['watch-project']119 expect(calls[0][0][0]).toEqual('watch-project');120 expect(calls[1][0][0]).toEqual('watch-project');121 // Call 2 is the query122 const query = calls[2][0];123 expect(query[0]).toEqual('query');124 expect(query[2].expression).toEqual([125 'allof',126 ['type', 'f'],127 ['anyof', ['suffix', 'js'], ['suffix', 'json']],128 ['anyof', ['dirname', 'fruits'], ['dirname', 'vegetables']],129 ]);130 expect(query[2].fields).toEqual(['name', 'exists', 'mtime_ms']);131 expect(query[2].glob).toEqual([132 'fruits/**/*.js',133 'fruits/**/*.json',134 'vegetables/**/*.js',135 'vegetables/**/*.json',136 ]);137 expect(data.clocks).toEqual(138 createMap({139 '': 'c:fake-clock:1',140 }),141 );142 expect(data.files).toEqual(mockFiles);143 expect(client.end).toBeCalled();144 }));145 test('applies the mapper when needed', () => {146 mockResponse = {147 'list-capabilities': {148 [undefined]: {149 capabilities: ['field-content.sha1hex'],150 },151 },152 query: {153 [ROOT_MOCK]: {154 clock: 'c:fake-clock:1',155 files: [156 {157 exists: true,158 mtime_ms: {toNumber: () => 33},159 name: 'vegetables/durian.zip',160 },161 ],162 is_fresh_instance: true,163 version: '4.5.0',164 },165 },166 'watch-project': WATCH_PROJECT_MOCK,167 };168 return watchmanCrawl({169 data: {170 clocks: new Map(),171 files: new Map(),172 },173 extensions: ['js', 'json', 'zip'],174 ignore: pearMatcher,175 mapper: n =>176 n.endsWith('.zip')177 ? [path.join(n, 'foo.1.js'), path.join(n, 'foo.2.js')]178 : null,179 rootDir: ROOT_MOCK,180 roots: ROOTS,181 }).then(data => {182 expect(data.files).toEqual(183 createMap({184 [path.join(DURIAN_RELATIVE, 'foo.1.js')]: ['', 33, 0, [], null],185 [path.join(DURIAN_RELATIVE, 'foo.2.js')]: ['', 33, 0, [], null],186 }),187 );188 });189 });190 test('updates the file object when the clock is given', () => {191 mockResponse = {192 'list-capabilities': {193 [undefined]: {194 capabilities: ['field-content.sha1hex'],195 },196 },197 query: {198 [ROOT_MOCK]: {199 clock: 'c:fake-clock:2',200 files: [201 {202 exists: true,203 mtime_ms: {toNumber: () => 42},204 name: 'fruits/kiwi.js',205 },206 {207 exists: false,208 mtime_ms: null,209 name: 'fruits/tomato.js',210 },211 ],212 is_fresh_instance: false,213 version: '4.5.0',214 },215 },216 'watch-project': WATCH_PROJECT_MOCK,217 };218 const clocks = createMap({219 '': 'c:fake-clock:1',220 });221 return watchmanCrawl({222 data: {223 clocks,224 files: mockFiles,225 },226 extensions: ['js', 'json'],227 ignore: pearMatcher,228 rootDir: ROOT_MOCK,229 roots: ROOTS,230 }).then(data => {231 // The object was reused.232 expect(data.files).toBe(mockFiles);233 expect(data.clocks).toEqual(234 createMap({235 '': 'c:fake-clock:2',236 }),237 );238 expect(data.files).toEqual(239 createMap({240 [KIWI_RELATIVE]: ['', 42, 0, [], null],241 [MELON_RELATIVE]: ['', 33, 0, [], null],242 [STRAWBERRY_RELATIVE]: ['', 30, 0, [], null],243 }),244 );245 });246 });247 test('resets the file object when watchman is restarted', () => {248 const mockTomatoSha1 = '321f6b7e8bf7f29aab89c5e41a555b1b0baa41a9';249 mockResponse = {250 'list-capabilities': {251 [undefined]: {252 capabilities: ['field-content.sha1hex'],253 },254 },255 query: {256 [ROOT_MOCK]: {257 clock: 'c:fake-clock:3',258 files: [259 {260 exists: true,261 mtime_ms: {toNumber: () => 42},262 name: 'fruits/kiwi.js',263 },264 {265 exists: true,266 mtime_ms: {toNumber: () => 41},267 name: 'fruits/banana.js',268 },269 {270 'content.sha1hex': mockTomatoSha1,271 exists: true,272 mtime_ms: {toNumber: () => 76},273 name: 'fruits/tomato.js',274 },275 ],276 is_fresh_instance: true,277 version: '4.5.0',278 },279 },280 'watch-project': WATCH_PROJECT_MOCK,281 };282 const mockBananaMetadata = ['Banana', 41, 1, ['Raspberry'], null];283 mockFiles.set(BANANA_RELATIVE, mockBananaMetadata);284 const mockTomatoMetadata = ['Tomato', 31, 1, [], mockTomatoSha1];285 mockFiles.set(TOMATO_RELATIVE, mockTomatoMetadata);286 const clocks = createMap({287 '': 'c:fake-clock:1',288 });289 return watchmanCrawl({290 data: {291 clocks,292 files: mockFiles,293 },294 extensions: ['js', 'json'],295 ignore: pearMatcher,296 rootDir: ROOT_MOCK,297 roots: ROOTS,298 }).then(data => {299 // The file object was *not* reused.300 expect(data.files).not.toBe(mockFiles);301 expect(data.clocks).toEqual(302 createMap({303 '': 'c:fake-clock:3',304 }),305 );306 // /fruits/strawberry.js was removed from the file list.307 expect(data.files).toEqual(308 createMap({309 [BANANA_RELATIVE]: mockBananaMetadata,310 [KIWI_RELATIVE]: ['', 42, 0, [], null],311 [TOMATO_RELATIVE]: ['Tomato', 76, 1, [], mockTomatoSha1],312 }),313 );314 // Even though the file list was reset, old file objects are still reused315 // if no changes have been made316 expect(data.files.get(BANANA_RELATIVE)).toBe(mockBananaMetadata);317 // Old file objects are not reused if they have a different mtime318 expect(data.files.get(TOMATO_RELATIVE)).not.toBe(mockTomatoMetadata);319 });320 });321 test('properly resets the file map when only one watcher is reset', () => {322 mockResponse = {323 'list-capabilities': {324 [undefined]: {325 capabilities: ['field-content.sha1hex'],326 },327 },328 query: {329 [FRUITS]: {330 clock: 'c:fake-clock:3',331 files: [332 {333 exists: true,334 mtime_ms: {toNumber: () => 42},335 name: 'kiwi.js',336 },337 ],338 is_fresh_instance: false,339 version: '4.5.0',340 },341 [VEGETABLES]: {342 clock: 'c:fake-clock:4',343 files: [344 {345 exists: true,346 mtime_ms: {toNumber: () => 33},347 name: 'melon.json',348 },349 ],350 is_fresh_instance: true,351 version: '4.5.0',352 },353 },354 'watch-project': {355 [FRUITS]: {356 watch: forcePOSIXPaths(FRUITS),357 },358 [VEGETABLES]: {359 watch: forcePOSIXPaths(VEGETABLES),360 },361 },362 };363 const clocks = createMap({364 [FRUITS_RELATIVE]: 'c:fake-clock:1',365 [VEGETABLES_RELATIVE]: 'c:fake-clock:2',366 });367 return watchmanCrawl({368 data: {369 clocks,370 files: mockFiles,371 },372 extensions: ['js', 'json'],373 ignore: pearMatcher,374 rootDir: ROOT_MOCK,375 roots: ROOTS,376 }).then(data => {377 expect(data.clocks).toEqual(378 createMap({379 [FRUITS_RELATIVE]: 'c:fake-clock:3',380 [VEGETABLES_RELATIVE]: 'c:fake-clock:4',381 }),382 );383 expect(data.files).toEqual(384 createMap({385 [KIWI_RELATIVE]: ['', 42, 0, [], null],386 [MELON_RELATIVE]: ['', 33, 0, [], null],387 }),388 );389 });390 });391 test('does not add directory filters to query when watching a ROOT', () => {392 mockResponse = {393 'list-capabilities': {394 [undefined]: {395 capabilities: ['field-content.sha1hex'],396 },397 },398 query: {399 [ROOT_MOCK]: {400 clock: 'c:fake-clock:1',401 files: [],402 is_fresh_instance: false,403 version: '4.5.0',404 },405 },406 'watch-project': {407 [FRUITS]: {408 relative_path: 'fruits',409 watch: forcePOSIXPaths(ROOT_MOCK),410 },411 [ROOT_MOCK]: {412 watch: forcePOSIXPaths(ROOT_MOCK),413 },414 [VEGETABLES]: {415 relative_path: 'vegetables',416 watch: forcePOSIXPaths(ROOT_MOCK),417 },418 },419 };420 return watchmanCrawl({421 data: {422 clocks: new Map(),423 files: new Map(),424 },425 extensions: ['js', 'json'],426 ignore: pearMatcher,427 rootDir: ROOT_MOCK,428 roots: [...ROOTS, ROOT_MOCK],429 }).then(data => {430 const client = watchman.Client.mock.instances[0];431 const calls = client.command.mock.calls;432 expect(client.on).toBeCalled();433 expect(client.on).toBeCalledWith('error', expect.any(Function));434 // First 3 calls are for ['watch-project']435 expect(calls[0][0][0]).toEqual('watch-project');436 expect(calls[1][0][0]).toEqual('watch-project');437 expect(calls[2][0][0]).toEqual('watch-project');438 // Call 4 is the query439 const query = calls[3][0];440 expect(query[0]).toEqual('query');441 expect(query[2].expression).toEqual([442 'allof',443 ['type', 'f'],444 ['anyof', ['suffix', 'js'], ['suffix', 'json']],445 ]);446 expect(query[2].fields).toEqual(['name', 'exists', 'mtime_ms']);447 expect(query[2].glob).toEqual(['**/*.js', '**/*.json']);448 expect(data.clocks).toEqual(449 createMap({450 '': 'c:fake-clock:1',451 }),452 );453 expect(data.files).toEqual(createMap({}));454 expect(client.end).toBeCalled();455 });456 });457 test('SHA-1 requested and available', async () => {458 mockResponse = {459 'list-capabilities': {460 [undefined]: {461 capabilities: ['field-content.sha1hex'],462 },463 },464 query: {465 [ROOT_MOCK]: {466 clock: 'c:fake-clock:1',467 files: [],468 is_fresh_instance: false,469 version: '4.5.0',470 },471 },472 'watch-project': {473 [ROOT_MOCK]: {474 watch: forcePOSIXPaths(ROOT_MOCK),475 },476 },477 };478 await watchmanCrawl({479 computeSha1: true,480 data: {481 clocks: new Map(),482 files: new Map(),483 },484 extensions: ['js', 'json'],485 rootDir: ROOT_MOCK,486 roots: [ROOT_MOCK],487 });488 const client = watchman.Client.mock.instances[0];489 const calls = client.command.mock.calls;490 expect(calls[0][0]).toEqual(['list-capabilities']);491 expect(calls[2][0][2].fields).toContain('content.sha1hex');492 });493 test('SHA-1 requested and NOT available', async () => {494 mockResponse = {495 'list-capabilities': {496 [undefined]: {497 capabilities: [],498 },499 },500 query: {501 [ROOT_MOCK]: {502 clock: 'c:fake-clock:1',503 files: [],504 is_fresh_instance: false,505 version: '4.5.0',506 },507 },508 'watch-project': {509 [ROOT_MOCK]: {510 watch: forcePOSIXPaths(ROOT_MOCK),511 },512 },513 };514 await watchmanCrawl({515 computeSha1: true,516 data: {517 clocks: new Map(),518 files: new Map(),519 },520 extensions: ['js', 'json'],521 rootDir: ROOT_MOCK,522 roots: [ROOT_MOCK],523 });524 const client = watchman.Client.mock.instances[0];525 const calls = client.command.mock.calls;526 expect(calls[0][0]).toEqual(['list-capabilities']);527 expect(calls[2][0][2].fields).not.toContain('content.sha1hex');528 });...
watchman-test.js
Source:watchman-test.js
...77 const watchman = require('fb-watchman');78 const path = require('../../fastpath');79 const originalPathRelative = path.relative;80 path.relative = jest.fn(from => '/root-mock' + from);81 return watchmanCrawl(82 ['/fruits', '/vegetables'],83 ['js', 'json'],84 pearMatcher,85 {86 clocks: Object.create(null),87 files: Object.create(null),88 }89 ).then(data => {90 const client = watchman.Client.mock.instances[0];91 const calls = client.command.mock.calls;92 expect(client.on).toBeCalled();93 expect(client.on).toBeCalledWith('error', jasmine.any(Function));94 // Call 0 and 1 are for ['watch-project']95 expect(calls[0][0][0]).toEqual('watch-project');96 expect(calls[1][0][0]).toEqual('watch-project');97 // Calls 2 and 3 are queries98 const query1 = calls[2][0];99 const query2 = calls[3][0];100 expect(query1[0]).toEqual('query');101 expect(query2[0]).toEqual('query');102 expect(query1[2].expression).toEqual([103 'allof',104 ['type', 'f'],105 ['anyof', ['suffix', 'js'], ['suffix', 'json']],106 ['anyof', ['dirname', '/root-mock/fruits']],107 ]);108 expect(query2[2].expression).toEqual([109 'allof',110 ['type', 'f'],111 ['anyof', ['suffix', 'js'], ['suffix', 'json']],112 ['anyof', ['dirname', '/root-mock/vegetables']],113 ]);114 expect(query1[2].fields).toEqual(['name', 'exists', 'mtime_ms']);115 expect(query2[2].fields).toEqual(['name', 'exists', 'mtime_ms']);116 expect(query1[2].suffix).toEqual(['js', 'json']);117 expect(query2[2].suffix).toEqual(['js', 'json']);118 expect(data.clocks).toEqual({119 '/fruits': 'c:fake-clock:1',120 '/vegetables': 'c:fake-clock:2',121 });122 expect(data.files).toEqual(mockFiles);123 path.relative = originalPathRelative;124 expect(client.end).toBeCalled();125 });126 });127 it('updates the file object when the clock is given', () => {128 mockResponse = {129 '/fruits': {130 version: '4.5.0',131 clock: 'c:fake-clock:3',132 is_fresh_instance: false,133 files: [134 {135 name: 'kiwi.js',136 exists: true,137 mtime_ms: {toNumber: () => 42},138 },139 {140 name: 'tomato.js',141 exists: false,142 mtime_ms: null,143 },144 ],145 },146 '/vegetables': {147 version: '4.5.0',148 clock: 'c:fake-clock:4',149 files: [],150 },151 };152 const clocks = Object.assign(Object.create(null), {153 '/fruits': 'c:fake-clock:1',154 '/vegetables': 'c:fake-clock:2',155 });156 return watchmanCrawl(157 ['/fruits', '/vegetables'],158 ['js', 'json'],159 pearMatcher,160 {161 clocks,162 files: mockFiles,163 }164 ).then(data => {165 // The object was reused.166 expect(data.files).toBe(mockFiles);167 expect(data.clocks).toEqual({168 '/fruits': 'c:fake-clock:3',169 '/vegetables': 'c:fake-clock:4',170 });171 expect(data.files).toEqual({172 '/fruits/strawberry.js': ['', 30, 0, []],173 '/fruits/kiwi.js': ['', 42, 0, []],174 '/vegetables/melon.json': ['', 33, 0, []],175 });176 });177 });178 it('resets the file object when watchman is restarted', () => {179 mockResponse = {180 '/fruits': {181 version: '4.5.0',182 clock: 'c:fake-clock:5',183 is_fresh_instance: true,184 files: [185 {186 name: 'kiwi.js',187 exists: true,188 mtime_ms: {toNumber: () => 42},189 },190 {191 name: 'banana.js',192 exists: true,193 mtime_ms: {toNumber: () => 41},194 },195 {196 name: 'tomato.js',197 exists: true,198 mtime_ms: {toNumber: () => 31},199 },200 ],201 },202 '/vegetables': {203 version: '4.5.0',204 clock: 'c:fake-clock:6',205 is_fresh_instance: true,206 files: [],207 },208 };209 const mockMetadata = ['Banana', 41, 1, ['Raspberry']];210 mockFiles['/fruits/banana.js'] = mockMetadata;211 const clocks = Object.assign(Object.create(null), {212 '/fruits': 'c:fake-clock:1',213 '/vegetables': 'c:fake-clock:2',214 });215 return watchmanCrawl(216 ['/fruits', '/vegetables'],217 ['js', 'json'],218 pearMatcher,219 {220 clocks,221 files: mockFiles,222 }223 ).then(data => {224 // The file object was *not* reused.225 expect(data.files).not.toBe(mockFiles);226 expect(data.clocks).toEqual({227 '/fruits': 'c:fake-clock:5',228 '/vegetables': 'c:fake-clock:6',229 });230 // /fruits/strawberry.js was removed from the file list.231 expect(data.files).toEqual({232 '/fruits/kiwi.js': ['', 42, 0, []],233 '/fruits/banana.js': mockMetadata,234 '/fruits/tomato.js': mockFiles['/fruits/tomato.js'],235 });236 // Even though the file list was reset, old file objects are still reused237 // if no changes have been made.238 expect(data.files['/fruits/banana.js']).toBe(mockMetadata);239 expect(data.files['/fruits/tomato.js'])240 .toBe(mockFiles['/fruits/tomato.js']);241 });242 });243 it('properly resets the file map when only one watcher is reset', () => {244 mockResponse = {245 '/fruits': {246 version: '4.5.0',247 clock: 'c:fake-clock:3',248 is_fresh_instance: false,249 files: [250 {251 name: 'kiwi.js',252 exists: true,253 mtime_ms: {toNumber: () => 42},254 },255 ],256 },257 '/vegetables': {258 version: '4.5.0',259 clock: 'c:fake-clock:4',260 is_fresh_instance: true,261 files: [262 {263 name: 'melon.json',264 exists: true,265 mtime_ms: {toNumber: () => 33},266 },267 ],268 },269 };270 const clocks = Object.assign(Object.create(null), {271 '/fruits': 'c:fake-clock:1',272 '/vegetables': 'c:fake-clock:2',273 });274 return watchmanCrawl(275 ['/fruits', '/vegetables'],276 ['js', 'json'],277 pearMatcher,278 {279 clocks,280 files: mockFiles,281 }282 ).then(data => {283 expect(data.clocks).toEqual({284 '/fruits': 'c:fake-clock:3',285 '/vegetables': 'c:fake-clock:4',286 });287 expect(data.files).toEqual({288 '/fruits/kiwi.js': ['', 42, 0, []],...
index.js
Source:index.js
...3const watchmanCrawl = require('./watchman');4function crawl(roots, options) {5 const {fileWatcher} = options;6 return (fileWatcher ? fileWatcher.isWatchman() : Promise.resolve(false)).then(7 isWatchman => isWatchman ? watchmanCrawl(roots, options) : nodeCrawl(roots, options)8 );9}...
LambdaTest’s Jest Testing Tutorial covers step-by-step guides around Jest with code examples to help you be proficient with the Jest framework. The Jest tutorial has chapters to help you learn right from the basics of Jest framework to code-based tutorials around testing react apps with Jest, perform snapshot testing, import ES modules and more.
|<p>it('check_object_of_Car', () => {</p><p>
expect(newCar()).toBeInstanceOf(Car);</p><p>
});</p>|
| :- |
Get 100 minutes of automation test minutes FREE!!