Skip to content

Commit

Permalink
feat(valid-expect-in-promise): re-write implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Sep 26, 2021
1 parent 6accb90 commit 2a88848
Show file tree
Hide file tree
Showing 2 changed files with 216 additions and 243 deletions.
281 changes: 166 additions & 115 deletions src/rules/__tests__/valid-expect-in-promise.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,35 @@ const ruleTester = new TSESLint.RuleTester({

ruleTester.run('valid-expect-in-promise', rule, {
valid: [
// todo: done callback
// dedent`
// it('it1', () => new Promise((done) => {
// test()
// .then(() => {
// expect(someThing).toEqual(true);
// done();
// });
// }));
// `,
dedent`
it('it1', () => new Promise((done) => {
test()
.then(() => {
expect(someThing).toEqual(true);
done();
});
}));
const myFn = () => {
Promise.resolve().then(() => {
expect(true).toBe(false);
});
};
`,
dedent`
const myFn = () => {
Promise.resolve().then(() => {
expect(true).toBe(false);
});
};
it('it1', () => {
return somePromise.then(() => {
expect(someThing).toEqual(true);
});
});
`,
dedent`
it('it1', () => {
Expand Down Expand Up @@ -74,50 +95,55 @@ ruleTester.run('valid-expect-in-promise', rule, {
});
});
`,
dedent`
it('it1', function () {
Promise.resolve().then(/*fulfillment*/ function () {
}, undefined, /*rejection*/ function () {
expect(someThing).toEqual(true)
})
});
`,
dedent`
it('it1', function () {
return Promise.resolve().then(function () {
/*fulfillment*/
}, function () {
/*rejection*/
expect(someThing).toEqual(true);
});
});
`,
// todo: tighter "is promise" check
// dedent`
// it('it1', function () {
// Promise.resolve().then(/*fulfillment*/ function () {
// }, undefined, /*rejection*/ function () {
// expect(someThing).toEqual(true)
// })
// });
// `,
// todo: tighter "is promise" check
// dedent`
// it('it1', function () {
// return Promise.resolve().then(function () {
// /*fulfillment*/
// }, function () {
// /*rejection*/
// expect(someThing).toEqual(true);
// });
// });
// `,
dedent`
it('it1', function () {
return somePromise.then()
});
`,
dedent`
it('it1', async () => {
await Promise.resolve().then(function () {
expect(someThing).toEqual(true)
});
});
`,
dedent`
it('it1', async () => {
await somePromise.then(() => {
expect(someThing).toEqual(true)
});
});
`,
dedent`
it('it1', async () => {
await getSomeThing().getPromise().then(function () {
expect(someThing).toEqual(true)
});
});
`,
// todo: async
// dedent`
// it('it1', async () => {
// await Promise.resolve().then(function () {
// expect(someThing).toEqual(true)
// });
// });
// `,
// todo: async
// dedent`
// it('it1', async () => {
// await somePromise.then(() => {
// expect(someThing).toEqual(true)
// });
// });
// `,
// todo: async
// dedent`
// it('it1', async () => {
// await getSomeThing().getPromise().then(function () {
// expect(someThing).toEqual(true)
// });
// });
// `,
dedent`
it('it1', () => {
return somePromise.then(() => {
Expand All @@ -138,24 +164,26 @@ ruleTester.run('valid-expect-in-promise', rule, {
})
});
`,
dedent`
test('later return', () => {
const promise = something().then(value => {
expect(value).toBe('red');
});
return promise;
});
`,
dedent`
test.only('later return', () => {
const promise = something().then(value => {
expect(value).toBe('red');
});
return promise;
});
`,
// todo: as variable
// dedent`
// test('later return', () => {
// const promise = something().then(value => {
// expect(value).toBe('red');
// });
//
// return promise;
// });
// `,
// todo: as variable
// dedent`
// test.only('later return', () => {
// const promise = something().then(value => {
// expect(value).toBe('red');
// });
//
// return promise;
// });
// `,
dedent`
it('shorthand arrow', () =>
something().then(value => {
Expand All @@ -165,34 +193,37 @@ ruleTester.run('valid-expect-in-promise', rule, {
})
);
`,
dedent`
it('promise test', () => {
const somePromise = getThatPromise();
somePromise.then((data) => {
expect(data).toEqual('foo');
});
expect(somePromise).toBeDefined();
return somePromise;
});
`,
dedent`
test('promise test', function () {
let somePromise = getThatPromise();
somePromise.then((data) => {
expect(data).toEqual('foo');
});
expect(somePromise).toBeDefined();
return somePromise;
});
`,
dedent`
it('crawls for files based on patterns', () => {
const promise = nodeCrawl({}).then(data => {
expect(childProcess.spawn).lastCalledWith('find');
});
return promise;
});
`,
// todo: as variable
// dedent`
// it('promise test', () => {
// const somePromise = getThatPromise();
// somePromise.then((data) => {
// expect(data).toEqual('foo');
// });
// expect(somePromise).toBeDefined();
// return somePromise;
// });
// `,
// todo: as variable
// dedent`
// test('promise test', function () {
// let somePromise = getThatPromise();
// somePromise.then((data) => {
// expect(data).toEqual('foo');
// });
// expect(somePromise).toBeDefined();
// return somePromise;
// });
// `,
// todo: as variable
// dedent`
// it('crawls for files based on patterns', () => {
// const promise = nodeCrawl({}).then(data => {
// expect(childProcess.spawn).lastCalledWith('find');
// });
// return promise;
// });
// `,
dedent`
it(
'test function',
Expand Down Expand Up @@ -226,20 +257,40 @@ ruleTester.run('valid-expect-in-promise', rule, {
}))
`,
'it("it1", () => somePromise.then(() => expect(someThing).toEqual(true)))',
dedent`
it('promise test with done', (done) => {
const promise = getPromise();
promise.then(() => expect(someThing).toEqual(true));
});
`,
dedent`
it('name of done param does not matter', (nameDoesNotMatter) => {
const promise = getPromise();
promise.then(() => expect(someThing).toEqual(true));
});
`,
// todo: done callback
// dedent`
// it('promise test with done', (done) => {
// const promise = getPromise();
// promise.then(() => expect(someThing).toEqual(true));
// });
// `,
// todo: done callback
// dedent`
// it('name of done param does not matter', (nameDoesNotMatter) => {
// const promise = getPromise();
// promise.then(() => expect(someThing).toEqual(true));
// });
// `,
],
invalid: [
{
code: dedent`
const myFn = () => {
Promise.resolve().then(() => {
expect(true).toBe(false);
});
};
it('it1', () => {
somePromise.then(() => {
expect(someThing).toEqual(true);
});
});
`,
errors: [
{ column: 3, endColumn: 6, messageId: 'returnPromise', line: 8 },
],
},
{
code: dedent`
it('it1', () => {
Expand All @@ -250,16 +301,16 @@ ruleTester.run('valid-expect-in-promise', rule, {
`,
errors: [{ column: 3, endColumn: 6, messageId: 'returnPromise' }],
},
// {
// code: `
// it('it1', () => {
// somePromise['then'](() => {
// expect(someThing).toEqual(true);
// });
// });
// `,
// errors: [{ column: 12, endColumn: 15, messageId: 'returnPromise' }],
// },
{
code: `
it('it1', () => {
somePromise['then'](() => {
expect(someThing).toEqual(true);
});
});
`,
errors: [{ column: 10, endColumn: 13, messageId: 'returnPromise' }],
},
{
code: dedent`
it('it1', function() {
Expand Down Expand Up @@ -361,7 +412,7 @@ ruleTester.run('valid-expect-in-promise', rule, {
});
});
`,
errors: [{ column: 9, endColumn: 5, messageId: 'returnPromise' }],
errors: [{ column: 3, endColumn: 6, messageId: 'returnPromise' }],
},
{
code: dedent`
Expand Down
Loading

0 comments on commit 2a88848

Please sign in to comment.