1414import * as path from 'path' ;
1515import { NamedBlock , ParseResult } from '../..' ;
1616import { parse } from '../../parsers/babel_parser' ;
17- import { parseOptions } from '../../parsers/helper' ;
17+ import { JESParserOptions , parseOptions } from '../../parsers/helper' ;
1818
1919const fixtures = path . resolve ( 'fixtures' ) ;
2020
@@ -26,10 +26,11 @@ describe('parsers', () => {
2626 ${ 'whatever.ts' }
2727 ${ 'whatever.tsx' }
2828 ` ( 'can parse file like $fileName' , ( { fileName} ) => {
29- const parseFunction = ( file : string , data ?: string ) => parse ( file , data , parseOptions ( fileName ) ) ;
29+ const parseFunction = ( file : string , data ?: string , options ?: JESParserOptions ) =>
30+ parse ( file , data , parseOptions ( fileName , options ) ) ;
3031 const assertBlock = ( block , start , end , name : string = null ) => {
31- expect ( block . start ) . toEqual ( start ) ;
32- expect ( block . end ) . toEqual ( end ) ;
32+ expect ( block . start ) . toEqual ( expect . objectContaining ( start ) ) ;
33+ expect ( block . end ) . toEqual ( expect . objectContaining ( end ) ) ;
3334 if ( name ) {
3435 expect ( block . name ) . toEqual ( name ) ;
3536 }
@@ -210,17 +211,17 @@ describe('parsers', () => {
210211 expect ( data . expects . length ) . toEqual ( 8 ) ;
211212
212213 const firstExpect = data . expects [ 0 ] ;
213- expect ( firstExpect . start ) . toEqual ( { column : 5 , line : 13 } ) ;
214- expect ( firstExpect . end ) . toEqual ( { column : 36 , line : 13 } ) ;
214+ expect ( firstExpect . start ) . toEqual ( expect . objectContaining ( { column : 5 , line : 13 } ) ) ;
215+ expect ( firstExpect . end ) . toEqual ( expect . objectContaining ( { column : 36 , line : 13 } ) ) ;
215216 } ) ;
216217
217218 it ( 'finds Expects in a danger flow test file ' , ( ) => {
218219 const data = parseFunction ( `${ fixtures } /dangerjs/github.example` ) ;
219220 expect ( data . expects . length ) . toEqual ( 3 ) ;
220221
221222 const thirdExpect = data . expects [ 2 ] ;
222- expect ( thirdExpect . start ) . toEqual ( { column : 5 , line : 33 } ) ;
223- expect ( thirdExpect . end ) . toEqual ( { column : 39 , line : 33 } ) ;
223+ expect ( thirdExpect . start ) . toEqual ( expect . objectContaining ( { column : 5 , line : 33 } ) ) ;
224+ expect ( thirdExpect . end ) . toEqual ( expect . objectContaining ( { column : 39 , line : 33 } ) ) ;
224225 } ) ;
225226
226227 it ( 'finds Expects in a metaphysics test file' , ( ) => {
@@ -373,7 +374,7 @@ describe('parsers', () => {
373374 startCol : number ,
374375 endLine : number ,
375376 endCol : number ,
376- nameType = 'Literal ' ,
377+ nameType = 'StringLiteral ' ,
377378 lastProperty : string = null
378379 ) => {
379380 expect ( nBlock . name ) . toEqual ( name ) ;
@@ -464,7 +465,7 @@ describe('parsers', () => {
464465
465466 const itBlock = parseResult . itBlocks [ 0 ] ;
466467 assertBlock2 ( itBlock , 2 , 7 , 4 , 9 , 'each test %p' ) ;
467- assertNameInfo ( itBlock , 'each test %p' , 2 , 33 , 2 , 44 , 'Literal ' , 'each' ) ;
468+ assertNameInfo ( itBlock , 'each test %p' , 2 , 33 , 2 , 44 , 'StringLiteral ' , 'each' ) ;
468469 } ) ;
469470 it ( 'should be able to detect it.skip.each' , ( ) => {
470471 const data = `
@@ -478,7 +479,7 @@ describe('parsers', () => {
478479
479480 const itBlock = parseResult . itBlocks [ 0 ] ;
480481 assertBlock2 ( itBlock , 2 , 7 , 4 , 9 , 'each test %p' ) ;
481- assertNameInfo ( itBlock , 'each test %p' , 2 , 38 , 2 , 49 , 'Literal ' , 'each' ) ;
482+ assertNameInfo ( itBlock , 'each test %p' , 2 , 38 , 2 , 49 , 'StringLiteral ' , 'each' ) ;
482483 } ) ;
483484
484485 it ( 'For the simplest it.each cases' , ( ) => {
@@ -933,6 +934,83 @@ describe('parsers', () => {
933934 expect ( parseResult . expects . length ) . toEqual ( 0 ) ;
934935 } ) ;
935936 } ) ;
937+ describe ( 'pluginOptions' , ( ) => {
938+ describe ( 'decorators' , ( ) => {
939+ it ( 'legacy' , ( ) => {
940+ const data = `
941+ import { asMockedFunction, type AnyFunction } from '@whatever/jest-types';
942+ test('a test', () => {
943+ expect(true).toBe(true);
944+ });
945+ class SimpleTestController {
946+ handlerMethod(@Body() xxx) {
947+ return;
948+ }
949+ }
950+ ` ;
951+ const parseResult = parseFunction ( 'whatever' , data , { plugins : { decorators : 'legacy' } } ) ;
952+ expect ( parseResult . itBlocks . length ) . toEqual ( 1 ) ;
953+
954+ const name = 'a test' ;
955+ const itBlock = parseResult . itBlocks [ 0 ] ;
956+ assertBlock2 ( itBlock , 3 , 11 , 5 , 13 , name ) ;
957+ assertNameInfo ( itBlock , name , 3 , 17 , 3 , 22 ) ;
958+ } ) ;
959+ it ( 'decoratorsBeforeExport' , ( ) => {
960+ const beforeExport = `
961+ test('a test', () => {
962+ expect(true).toBe(true);
963+ });
964+ @dec
965+ export class C {}
966+ ` ;
967+ const afterExport = `
968+ test('a test', () => {
969+ expect(true).toBe(true);
970+ });
971+ export @dec class C {}
972+ ` ;
973+ const beforePlugin = { decorators : { decoratorsBeforeExport : true } } ;
974+ const afterPlugin = { decorators : { decoratorsBeforeExport : false } } ;
975+
976+ let parseResult = parseFunction ( 'whatever' , beforeExport , { plugins : beforePlugin } ) ;
977+ expect ( parseResult . itBlocks . length ) . toEqual ( 1 ) ;
978+
979+ parseResult = parseFunction ( 'whatever' , afterExport , { plugins : afterPlugin } ) ;
980+ expect ( parseResult . itBlocks . length ) . toEqual ( 1 ) ;
981+
982+ expect ( ( ) => parseFunction ( 'whatever' , beforeExport , { plugins : afterPlugin } ) ) . toThrow ( ) ;
983+ expect ( ( ) => parseFunction ( 'whatever' , afterExport , { plugins : beforePlugin } ) ) . toThrow ( ) ;
984+ } ) ;
985+ it ( 'allowCallParenthesized' , ( ) => {
986+ const callParenthesized = `
987+ test('a test', () => {
988+ expect(true).toBe(true);
989+ });
990+ @(dec)() class C {};
991+ ` ;
992+ const callNotParenthesized = `
993+ test('a test', () => {
994+ expect(true).toBe(true);
995+ });
996+ @(dec()) class C {};
997+ ` ;
998+
999+ const allowPlugin = { decorators : { allowCallParenthesized : true } } ;
1000+ const notAllowPlugin = { decorators : { allowCallParenthesized : false } } ;
1001+
1002+ let parseResult = parseFunction ( 'whatever' , callParenthesized , { plugins : allowPlugin } ) ;
1003+ expect ( parseResult . itBlocks . length ) . toEqual ( 1 ) ;
1004+
1005+ parseResult = parseFunction ( 'whatever' , callNotParenthesized , { plugins : allowPlugin } ) ;
1006+ expect ( parseResult . itBlocks . length ) . toEqual ( 1 ) ;
1007+ parseResult = parseFunction ( 'whatever' , callNotParenthesized , { plugins : notAllowPlugin } ) ;
1008+ expect ( parseResult . itBlocks . length ) . toEqual ( 1 ) ;
1009+
1010+ expect ( ( ) => parseFunction ( 'whatever' , callParenthesized , { plugins : notAllowPlugin } ) ) . toThrow ( ) ;
1011+ } ) ;
1012+ } ) ;
1013+ } ) ;
9361014 describe ( 'parse error use case' , ( ) => {
9371015 it ( 'https://github.com/jest-community/vscode-jest/issues/405' , ( ) => {
9381016 const data = `
@@ -1023,6 +1101,26 @@ describe('parsers', () => {
10231101 const parseResult = parseFunction ( 'whatever' , data ) ;
10241102 expect ( parseResult . itBlocks . length ) . toEqual ( 1 ) ;
10251103
1104+ const name = 'a test' ;
1105+ const itBlock = parseResult . itBlocks [ 0 ] ;
1106+ assertBlock2 ( itBlock , 3 , 9 , 5 , 11 , name ) ;
1107+ assertNameInfo ( itBlock , name , 3 , 15 , 3 , 20 ) ;
1108+ } ) ;
1109+ it ( 'https://github.com/jest-community/jest-editor-support/issues/89' , ( ) => {
1110+ const data = `
1111+ import { asMockedFunction, type AnyFunction } from '@whatever/jest-types';
1112+ test('a test', () => {
1113+ expect(true).toBe(true);
1114+ });
1115+ class SimpleTestController {
1116+ handlerMethod(@Body() xxx) {
1117+ return;
1118+ }
1119+ }
1120+ ` ;
1121+ const parseResult = parseFunction ( 'whatever' , data , { plugins : { decorators : 'legacy' } } ) ;
1122+ expect ( parseResult . itBlocks . length ) . toEqual ( 1 ) ;
1123+
10261124 const name = 'a test' ;
10271125 const itBlock = parseResult . itBlocks [ 0 ] ;
10281126 assertBlock2 ( itBlock , 3 , 9 , 5 , 11 , name ) ;
0 commit comments