Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: update js linting #461

Merged
merged 11 commits into from
Apr 6, 2024
Merged
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
lib/*
node_modules/*
vendor/*
spec/*
12 changes: 11 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,15 @@ module.exports = {
env: {
browser: true,
node: true
}
},
overrides: [
{
files: ["test/**"],
// todo: these should be sourced from eslint-plugin-jest
env: { jest: true },
rules: {
'global-require': 'off'
}
}
]
}
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"lib/install/config/shakapacker.yml"
],
"scripts": {
"lint": "eslint package/",
"lint": "eslint .",
"test": "jest"
},
"dependencies": {
Expand All @@ -30,12 +30,13 @@
"babel-loader": "^8.2.4",
"compression-webpack-plugin": "^9.0.0",
"esbuild-loader": "^2.18.0",
"eslint": "^7.32.0",
"eslint-config-airbnb": "^18.2.1",
"eslint-config-prettier": "^8.3.0",
"eslint": "^8.0.0",
"eslint-config-airbnb": "^19.0.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-import": "^2.24.2",
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-react": "^7.26.0",
"eslint-plugin-react-hooks": "^4.6.0",
"jest": "^28.1.3",
"memory-fs": "^0.5.0",
"swc-loader": "^0.1.15",
Expand Down
64 changes: 32 additions & 32 deletions test/helpers.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
const webpack = require("webpack");
const MemoryFS = require("memory-fs");
const thenify = require("thenify");
const path = require("path");
const webpack = require("webpack")
const MemoryFS = require("memory-fs")
const thenify = require("thenify")
const path = require("path")

const createTrackLoader = () => {
const filesTracked = {};
const filesTracked = {}
return [
filesTracked,
(source) => {
filesTracked[source.resource] = true;
return source;
},
];
};
filesTracked[source.resource] = true
return source
}
]
}

const node_modules = path.resolve("node_modules");
const node_modules_included = path.resolve("node_modules/included");
const app_javascript = path.resolve("app/javascript");
const pathToNodeModules = path.resolve("node_modules")
const pathToNodeModulesIncluded = path.resolve("node_modules/included")
const pathToAppJavascript = path.resolve("app/javascript")

const createInMemoryFs = () => {
const fs = new MemoryFS();
const fs = new MemoryFS()

fs.mkdirpSync(node_modules);
fs.mkdirpSync(node_modules_included);
fs.mkdirpSync(app_javascript);
fs.mkdirpSync(pathToNodeModules)
fs.mkdirpSync(pathToNodeModulesIncluded)
fs.mkdirpSync(pathToAppJavascript)

return fs;
};
return fs
}

const createTestCompiler = (config, fs = createInMemoryFs()) => {
Object.values(config.entry).forEach((file) => {
fs.writeFileSync(file, "console.log(1);");
});
fs.writeFileSync(file, "console.log(1);")
})

const compiler = webpack(config);
compiler.run = thenify(compiler.run);
compiler.inputFileSystem = fs;
compiler.outputFileSystem = fs;
return compiler;
};
const compiler = webpack(config)
compiler.run = thenify(compiler.run)
compiler.inputFileSystem = fs
compiler.outputFileSystem = fs
return compiler
}

const chdirTestApp = () => {
try {
Expand All @@ -59,9 +59,9 @@ module.exports = {
chdirCwd,
resetEnv,
createTrackLoader,
node_modules,
node_modules_included,
app_javascript,
pathToNodeModules,
pathToNodeModulesIncluded,
pathToAppJavascript,
createInMemoryFs,
createTestCompiler,
};
createTestCompiler
}
10 changes: 5 additions & 5 deletions test/package/config.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
/* global test expect, describe */

const { chdirTestApp, resetEnv } = require('../helpers')
const { resolve } = require('path')
const { chdirTestApp, resetEnv } = require('../helpers')

const rootPath = process.cwd()
chdirTestApp()

const config = require('../../package/config')

describe('Config', () => {
beforeEach(() => jest.resetModules() && resetEnv())
afterAll(() => process.chdir(rootPath))
Expand All @@ -26,6 +22,8 @@ describe('Config', () => {
})

test('should return additional paths as listed in app config, with resolved paths', () => {
const config = require('../../package/config')

expect(config.additional_paths).toEqual([
'app/assets',
'/etc/yarn',
Expand All @@ -35,6 +33,8 @@ describe('Config', () => {
})

test('should default manifestPath to the public dir', () => {
const config = require('../../package/config')

expect(config.manifestPath).toEqual(resolve('public/packs/manifest.json'))
})

Expand Down
2 changes: 0 additions & 2 deletions test/package/dev_server.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* global test expect, describe */

const { chdirTestApp } = require('../helpers')

const rootPath = process.cwd()
Expand Down
2 changes: 0 additions & 2 deletions test/package/development.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* test expect, describe, afterAll, beforeEach */

const { resolve } = require('path')
const { chdirTestApp, resetEnv } = require('../helpers')

Expand Down
2 changes: 0 additions & 2 deletions test/package/env.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* global test expect, describe */

const { chdirTestApp } = require('../helpers')

const rootPath = process.cwd()
Expand Down
25 changes: 11 additions & 14 deletions test/package/environments/base.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
/* global test expect, describe, afterAll, beforeEach */

// environment.js expects to find config/shakapacker.yml and resolved modules from
// the root of a Rails project

const { chdirTestApp, chdirCwd, resetEnv } = require('../../helpers')
const { resolve } = require('path')
const { chdirTestApp, resetEnv } = require('../../helpers')

const rootPath = process.cwd()
chdirTestApp()

const { resolve } = require('path')

const baseConfig = require('../../../package/environments/base')
const config = require("../../../package/config");
const config = require("../../../package/config")

describe('Base config', () => {
beforeEach(() => jest.resetModules() && resetEnv())
Expand All @@ -30,9 +27,9 @@ describe('Base config', () => {

test('should return true for css_extract_ignore_order_warnings when configured', () => {
process.env.SHAKAPACKER_CONFIG = 'config/shakapacker_css_extract_ignore_order_warnings.yml'
const config = require("../../../package/config");
const config2 = require("../../../package/config")

expect(config.css_extract_ignore_order_warnings).toEqual(true)
expect(config2.css_extract_ignore_order_warnings).toEqual(true)
})

test('should return only 2 entry points with config.nested_entries == false', () => {
Expand All @@ -47,19 +44,19 @@ describe('Base config', () => {

test('should returns top level and nested entry points with config.nested_entries == true', () => {
process.env.SHAKAPACKER_CONFIG = 'config/shakapacker_nested_entries.yml'
const config = require("../../../package/config");
const baseConfig = require('../../../package/environments/base')
const config2 = require("../../../package/config")
const baseConfig2 = require('../../../package/environments/base')

expect(config.nested_entries).toEqual(true)
expect(config2.nested_entries).toEqual(true)

expect(baseConfig.entry.application).toEqual(
expect(baseConfig2.entry.application).toEqual(
resolve('app', 'javascript', 'entrypoints', 'application.js')
)
expect(baseConfig.entry.multi_entry.sort()).toEqual([
expect(baseConfig2.entry.multi_entry.sort()).toEqual([
resolve('app', 'javascript', 'entrypoints', 'multi_entry.css'),
resolve('app', 'javascript', 'entrypoints', 'multi_entry.js')
])
expect(baseConfig.entry['generated/something']).toEqual(
expect(baseConfig2.entry['generated/something']).toEqual(
resolve('app', 'javascript', 'entrypoints', 'generated', 'something.js')
)
})
Expand Down
11 changes: 5 additions & 6 deletions test/package/environments/development.test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
/* global test expect, describe, afterAll, beforeEach */

const { chdirTestApp, resetEnv } = require('../../helpers')

const rootPath = process.cwd()
chdirTestApp()

describe('Development specific config', () => {
beforeEach(() => {
jest.resetModules()
resetEnv()
process.env['NODE_ENV'] = 'development'
process.env.NODE_ENV = 'development'
})
afterAll(() => process.chdir(rootPath))

describe('with config.useContentHash = true', () => {
test('sets filename to use contentHash', () => {
const config = require("../../../package/config");
const config = require("../../../package/config")
config.useContentHash = true
const environmentConfig = require('../../../package/environments/development')

Expand All @@ -27,7 +26,7 @@ describe('Development specific config', () => {

describe('with config.useContentHash = false', () => {
test('sets filename without using contentHash', () => {
const config = require("../../../package/config");
const config = require("../../../package/config")
config.useContentHash = false
const environmentConfig = require('../../../package/environments/development')

Expand All @@ -40,7 +39,7 @@ describe('Development specific config', () => {

describe('with unset config.useContentHash', () => {
test('sets filename without using contentHash', () => {
const config = require("../../../package/config");
const config = require("../../../package/config")
delete config.useContentHash
const environmentConfig = require('../../../package/environments/development')

Expand Down
32 changes: 17 additions & 15 deletions test/package/environments/production.test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
/* global test expect, describe, afterAll, beforeEach */

const { chdirTestApp, resetEnv } = require('../../helpers')

const rootPath = process.cwd()
chdirTestApp()

describe('Production specific config', () => {
beforeEach(() => {
jest.resetModules()
resetEnv()
process.env['NODE_ENV'] = 'production'
process.env.NODE_ENV = 'production'
})
afterAll(() => process.chdir(rootPath))

describe('with config.useContentHash = true', () => {
test('sets filename to use contentHash', () => {
const config = require("../../../package/config");
const config = require("../../../package/config")
config.useContentHash = true
const environmentConfig = require('../../../package/environments/production')

Expand All @@ -25,10 +24,11 @@ describe('Production specific config', () => {
})

test("doesn't shows any warning message", () => {
const consoleWarnSpy = jest.spyOn(console, 'warn');
const config = require("../../../package/config");
const consoleWarnSpy = jest.spyOn(console, 'warn')
const config = require("../../../package/config")
config.useContentHash = true
const environmentConfig = require('../../../package/environments/production')

require('../../../package/environments/production')

expect(consoleWarnSpy).not.toHaveBeenCalledWith(
expect.stringMatching(/Setting 'useContentHash' to 'false' in the production environment/)
Expand All @@ -40,7 +40,7 @@ describe('Production specific config', () => {

describe('with config.useContentHash = false', () => {
test('sets filename to use contentHash', () => {
const config = require("../../../package/config");
const config = require("../../../package/config")
config.useContentHash = false
const environmentConfig = require('../../../package/environments/production')

Expand All @@ -51,10 +51,11 @@ describe('Production specific config', () => {
})

test('shows a warning message', () => {
const consoleWarnSpy = jest.spyOn(console, 'warn');
const config = require("../../../package/config");
const consoleWarnSpy = jest.spyOn(console, 'warn')
const config = require("../../../package/config")
config.useContentHash = false
const environmentConfig = require('../../../package/environments/production')

require('../../../package/environments/production')

expect(consoleWarnSpy).toHaveBeenCalledWith(
expect.stringMatching(/Setting 'useContentHash' to 'false' in the production environment/)
Expand All @@ -66,7 +67,7 @@ describe('Production specific config', () => {

describe('with unset config.useContentHash', () => {
test('sets filename to use contentHash', () => {
const config = require("../../../package/config");
const config = require("../../../package/config")
delete config.useContentHash
const environmentConfig = require('../../../package/environments/production')

Expand All @@ -77,10 +78,11 @@ describe('Production specific config', () => {
})

test("doesn't shows any warning message", () => {
const consoleWarnSpy = jest.spyOn(console, 'warn');
const config = require("../../../package/config");
const consoleWarnSpy = jest.spyOn(console, 'warn')
const config = require("../../../package/config")
delete config.useContentHash
const environmentConfig = require('../../../package/environments/production')

require('../../../package/environments/production')

expect(consoleWarnSpy).not.toHaveBeenCalledWith(
expect.stringMatching(/Setting 'useContentHash' to 'false' in the production environment/)
Expand Down
7 changes: 3 additions & 4 deletions test/package/index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const index = require('../../package/index')
const { generateWebpackConfig } = require('../helpers')

describe('index', () => {
test('exports webpack-merge v5 functions', () => {
Expand All @@ -25,9 +24,9 @@ describe('index', () => {
const { generateWebpackConfig } = require('../../package/index')

const webpackConfig = generateWebpackConfig({
newKey: 'new value',
output: {
path: 'new path'
newKey: 'new value',
output: {
path: 'new path'
}
})

Expand Down
Loading
Loading