Skip to content

Commit

Permalink
fix(jasmine-this): prevent TS annotations in non-TS files (#624)
Browse files Browse the repository at this point in the history
  • Loading branch information
puglyfe authored Oct 8, 2024
1 parent 34e7f02 commit a21e985
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 10 deletions.
51 changes: 49 additions & 2 deletions src/transformers/jasmine-this.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ describe('foo', () => {
)
})

test('adds any type to the test context with typescript (tsx)', () => {
test('does not add any type to the test context with javascript', () => {
expectTransformation(
`
beforeEach(function () {
Expand All @@ -535,6 +535,50 @@ test('adds any type to the test context with typescript (tsx)', () => {
});
`,
`
let testContext;
beforeEach(() => {
testContext = {};
});
beforeEach(() => {
testContext.hello = 'hi';
});
afterEach(() => {
console.log(testContext.hello);
});
describe('context', () => {
it('should work', () => {
console.log(testContext.hello);
});
});
`
)
})

test('adds any type to the test context with typescript (tsx)', () => {
expectTransformation(
{
path: 'test.tsx',
source: `
beforeEach(function () {
this.hello = 'hi';
});
afterEach(function () {
console.log(this.hello);
});
describe('context', () => {
it('should work', function () {
console.log(this.hello);
});
});
`,
},
`
let testContext: any;
beforeEach(() => {
Expand All @@ -561,7 +605,9 @@ test('adds any type to the test context with typescript (tsx)', () => {

test('adds any type to the test context with typescript (ts)', () => {
expectTransformation(
`
{
path: 'test.ts',
source: `
beforeEach(function () {
this.hello = 'hi';
});
Expand All @@ -576,6 +622,7 @@ describe('context', () => {
});
});
`,
},
`
let testContext: any;
Expand Down
7 changes: 4 additions & 3 deletions src/transformers/jasmine-this.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,10 @@ const jasmineThis: jscodeshift.Transform = (fileInfo, api, options) => {
j.variableDeclarator(
j.identifier.from({
name: contextName,
typeAnnotation: ['ts', 'tsx'].includes(options.parser)
? j.typeAnnotation(j.anyTypeAnnotation())
: null,
typeAnnotation:
['ts', 'tsx'].includes(options.parser) && /\.tsx?$/.test(fileInfo.path)
? j.typeAnnotation(j.anyTypeAnnotation())
: null,
}),
null
),
Expand Down
53 changes: 51 additions & 2 deletions src/transformers/mocha.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ describe('suite', () => {
)
})

test('adds any type to the test context with typescript (tsx)', () => {
test('does not add any type to the test context with javascript', () => {
expectTransformation(
`
describe('describe', function () {
Expand All @@ -204,6 +204,52 @@ describe('describe', function () {
})
`,
`
describe('describe', () => {
let testContext;
beforeEach(() => {
testContext = {};
});
beforeEach(() => {
testContext.hello = 'hi';
});
describe('context', () => {
test('test', () => {
console.log(testContext.hello);
});
it('it', () => {
console.log(testContext.hello);
});
})
})
`
)
})

test('adds any type to the test context with typescript (tsx)', () => {
expectTransformation(
{
path: 'test.tsx',
source: `
describe('describe', function () {
beforeEach(function () {
this.hello = 'hi';
});
context('context', () => {
test('test', function () {
console.log(this.hello);
});
it('it', function () {
console.log(this.hello);
});
})
})
`,
},
`
describe('describe', () => {
let testContext: any;
Expand Down Expand Up @@ -231,7 +277,9 @@ describe('describe', () => {

test('adds any type to the test context with typescript (ts)', () => {
expectTransformation(
`
{
path: 'test.ts',
source: `
describe('describe', function () {
beforeEach(function () {
this.hello = 'hi';
Expand All @@ -247,6 +295,7 @@ describe('describe', function () {
})
})
`,
},
`
describe('describe', () => {
let testContext: any;
Expand Down
18 changes: 15 additions & 3 deletions src/utils/test-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,22 @@ export function api(options): jscodeshift.API {
}
}

export function runPlugin(plugin: jscodeshift.Transform, source: string, options = {}) {
return plugin({ source, path: 'test.js' }, api(options), options)
type File = {
path: string
source: string
}

export function runPlugin(
plugin: jscodeshift.Transform,
file: string | File,
options = {}
) {
const source = typeof file === 'string' ? file : file.source
const path = typeof file === 'string' ? 'test.js' : file.path
return plugin({ source, path }, api(options), options)
}

export function wrapPlugin(plugin: jscodeshift.Transform) {
return (source: string, options = {}) => runPlugin(plugin, source, options) || null
return (source: string | File, options = {}) =>
runPlugin(plugin, source, options) || null
}

0 comments on commit a21e985

Please sign in to comment.