Skip to content

Commit

Permalink
Upgrade test and fix single line edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornua committed Jan 5, 2024
1 parent 2de961b commit 06951af
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 36 deletions.
5 changes: 4 additions & 1 deletion lib/rules/consistent-spacing-between-blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ exports.create = function (context) {
node,
message: 'Expected line break before this statement.',
fix(fixer) {
return fixer.insertTextAfter(beforeToken, '\n');
return fixer.insertTextAfter(
beforeToken,
linesBetween === 0 ? '\n\n' : '\n'
);
}
});
}
Expand Down
91 changes: 56 additions & 35 deletions test/rules/consistent-spacing-between-blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,50 +10,50 @@ ruleTester.run('require-spacing-between-mocha-calls', rule, {
valid: [
// Basic describe block
`describe('My Test', () => {
it('does something', () => {});
});`,
it('does something', () => {});
});`,

// Proper line break before each block within describe
`describe('My Test', () => {
it('performs action one', () => {});
it('performs action two', () => {});
});`,
it('performs action one', () => {});
it('performs action two', () => {});
});`,

// Nested describe blocks with proper spacing
`describe('Outer block', () => {
describe('Inner block', () => {
it('performs an action', () => {});
});
describe('Inner block', () => {
it('performs an action', () => {});
});
afterEach(() => {});
});`,
afterEach(() => {});
});`,

// Describe block with comments
`describe('My Test With Comments', () => {
it('does something', () => {});
// Some comment
afterEach(() => {});
});`,
it('does something', () => {});
// Some comment
afterEach(() => {});
});`,

// Mocha functions outside of a describe block
`it('does something outside a describe block', () => {});
afterEach(() => {});`
afterEach(() => {});`
],

invalid: [
// Missing line break between it and afterEach
{
code: `describe('My Test', function () {
it('does something', () => {});
afterEach(() => {});
});`,
it('does something', () => {});
afterEach(() => {});
});`,
output: `describe('My Test', function () {
it('does something', () => {});
it('does something', () => {});
afterEach(() => {});
});`,
afterEach(() => {});
});`,
errors: [
{
message: 'Expected line break before this statement.',
Expand All @@ -65,14 +65,14 @@ ruleTester.run('require-spacing-between-mocha-calls', rule, {
// Missing line break between beforeEach and it
{
code: `describe('My Test', () => {
beforeEach(() => {});
it('does something', () => {});
});`,
beforeEach(() => {});
it('does something', () => {});
});`,
output: `describe('My Test', () => {
beforeEach(() => {});
beforeEach(() => {});
it('does something', () => {});
});`,
it('does something', () => {});
});`,
errors: [
{
message: 'Expected line break before this statement.',
Expand All @@ -84,14 +84,35 @@ ruleTester.run('require-spacing-between-mocha-calls', rule, {
// Missing line break after a variable declaration
{
code: `describe('Variable declaration', () => {
const a = 1;
it('uses a variable', () => {});
});`,
const a = 1;
it('uses a variable', () => {});
});`,
output: `describe('Variable declaration', () => {
const a = 1;
const a = 1;
it('uses a variable', () => {});
});`,
it('uses a variable', () => {});
});`,
errors: [
{
message: 'Expected line break before this statement.',
type: 'CallExpression'
}
]
},

// Blocks on the same line
{
code:
'describe(\'Same line blocks\', () => {' +
'it(\'block one\', () => {});' +
'it(\'block two\', () => {});' +
'});',
output:
'describe(\'Same line blocks\', () => {' +
'it(\'block one\', () => {});' +
'\n\n' +
'it(\'block two\', () => {});' +
'});',
errors: [
{
message: 'Expected line break before this statement.',
Expand Down

0 comments on commit 06951af

Please sign in to comment.