Skip to content

Commit

Permalink
feat!: move tests and helpers into (#459)
Browse files Browse the repository at this point in the history
* test: move into `test` directory

* test: update import paths

* test: update jest config

* refactor: move test helpers

* test: move jest config into dedicated file

* docs: add changelog entry

* chore: remove unneeded eslint ignore
  • Loading branch information
G-Rath authored Apr 5, 2024
1 parent b2c05a6 commit 173723a
Show file tree
Hide file tree
Showing 23 changed files with 99 additions and 100 deletions.
1 change: 0 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
lib/*
node_modules/*
vendor/*
**/__tests__/**/*
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ Changes since the last non-beta release.

- Remove `isArray` utility (just use `Array.isArray` directly) and renamed a few files [PR 454](https://github.com/shakacode/shakapacker/pull/454) by [G-Rath](https://github.com/g-rath).

- Make JavaScript test helper utilities internal (`chdirTestApp`, `chdirCwd`, `resetEnv`) [PR 458](https://github.com/shakacode/shakapacker/pull/458) by [G-Rath](https://github.com/g-rath).

## [v7.2.3] - March 23, 2024

### Added
Expand Down
7 changes: 7 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
roots: ["<rootDir>/test"],
testPathIgnorePatterns: [
"/__fixtures__/",
"/__utils__/"
]
}
10 changes: 0 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,6 @@
"lint": "eslint package/",
"test": "jest"
},
"jest": {
"roots": [
"<rootDir>/package"
],
"testPathIgnorePatterns": [
"/__fixtures__/",
"/__utils__/"
],
"testRegex": "(/__tests__/.*|(\\.|/))\\.jsx?$"
},
"dependencies": {
"js-yaml": "^4.1.0",
"path-complete-extname": "^1.0.0"
Expand Down
16 changes: 0 additions & 16 deletions package/utils/helpers.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@
const isBoolean = (str) => /^true/.test(str) || /^false/.test(str)
const chdirTestApp = () => {
try {
return process.chdir('spec/shakapacker/test_app')
} catch (e) {
return null
}
}

const chdirCwd = () => process.chdir(process.cwd())

const resetEnv = () => {
process.env = {}
}

const ensureTrailingSlash = (path) => (path.endsWith('/') ? path : `${path}/`)

Expand Down Expand Up @@ -55,12 +42,9 @@ const loaderMatches = (configLoader, loaderToCheck, fn) => {
}

module.exports = {
chdirTestApp,
chdirCwd,
isBoolean,
ensureTrailingSlash,
canProcess,
moduleExists,
resetEnv,
loaderMatches
}
17 changes: 17 additions & 0 deletions package/rules/__tests__/__utils__/webpack.js → test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,24 @@ const createTestCompiler = (config, fs = createInMemoryFs()) => {
return compiler;
};

const chdirTestApp = () => {
try {
return process.chdir('spec/shakapacker/test_app')
} catch (e) {
return null
}
}

const chdirCwd = () => process.chdir(process.cwd())

const resetEnv = () => {
process.env = {}
}

module.exports = {
chdirTestApp,
chdirCwd,
resetEnv,
createTrackLoader,
node_modules,
node_modules_included,
Expand Down
10 changes: 5 additions & 5 deletions package/__tests__/config.js → test/package/config.test.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
/* global test expect, describe */

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

const rootPath = process.cwd()
chdirTestApp()

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

describe('Config', () => {
beforeEach(() => jest.resetModules() && resetEnv())
afterAll(() => process.chdir(rootPath))

test('public path', () => {
process.env.RAILS_ENV = 'development'
const config = require('../config')
const config = require('../../package/config')
expect(config.publicPath).toEqual('/packs/')
})

test('public path with asset host', () => {
process.env.RAILS_ENV = 'development'
process.env.SHAKAPACKER_ASSET_HOST = 'http://foo.com/'
const config = require('../config')
const config = require('../../package/config')
expect(config.publicPath).toEqual('http://foo.com/packs/')
})

Expand All @@ -40,7 +40,7 @@ describe('Config', () => {

test('should allow overriding manifestPath', () => {
process.env.SHAKAPACKER_CONFIG = 'config/shakapacker_manifest_path.yml'
const config = require('../config')
const config = require('../../package/config')
expect(config.manifestPath).toEqual(resolve('app/javascript/manifest.json'))
})
})
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* global test expect, describe */

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

const rootPath = process.cwd()
chdirTestApp()
Expand All @@ -16,23 +16,23 @@ describe('DevServer', () => {
process.env.SHAKAPACKER_DEV_SERVER_PORT = 5000
process.env.SHAKAPACKER_DEV_SERVER_DISABLE_HOST_CHECK = false

const devServer = require('../dev_server')
const devServer = require('../../package/dev_server')
expect(devServer).toBeDefined()
expect(devServer.host).toEqual('0.0.0.0')
expect(devServer.port).toEqual('5000')
expect(devServer.disable_host_check).toBe(false)
})

test('with custom env prefix', () => {
const config = require('../config')
const config = require('../../package/config')
config.dev_server.env_prefix = 'TEST_SHAKAPACKER_DEV_SERVER'

process.env.NODE_ENV = 'development'
process.env.RAILS_ENV = 'development'
process.env.TEST_SHAKAPACKER_DEV_SERVER_HOST = '0.0.0.0'
process.env.TEST_SHAKAPACKER_DEV_SERVER_PORT = 5000

const devServer = require('../dev_server')
const devServer = require('../../package/dev_server')
expect(devServer).toBeDefined()
expect(devServer.host).toEqual('0.0.0.0')
expect(devServer.port).toEqual('5000')
Expand All @@ -41,6 +41,6 @@ describe('DevServer', () => {
test('with NODE_ENV and RAILS_ENV set to production', () => {
process.env.RAILS_ENV = 'production'
process.env.NODE_ENV = 'production'
expect(require('../dev_server')).toEqual({})
expect(require('../../package/dev_server')).toEqual({})
})
})
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* test expect, describe, afterAll, beforeEach */

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

const rootPath = process.cwd()
chdirTestApp()
Expand All @@ -17,7 +17,7 @@ describe('Development environment', () => {
process.env.RAILS_ENV = 'development'
process.env.NODE_ENV = 'development'
process.env.WEBPACK_SERVE = 'true'
const { generateWebpackConfig } = require('../index')
const { generateWebpackConfig } = require('../../package/index')

const webpackConfig = generateWebpackConfig()

Expand All @@ -29,7 +29,7 @@ describe('Development environment', () => {
process.env.RAILS_ENV = 'development'
process.env.NODE_ENV = 'development'
process.env.WEBPACK_SERVE = undefined
const { generateWebpackConfig } = require('../index')
const { generateWebpackConfig } = require('../../package/index')

const webpackConfig = generateWebpackConfig()

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

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

const rootPath = process.cwd()
chdirTestApp()
Expand All @@ -12,7 +12,7 @@ describe('Env', () => {
test('with NODE_ENV and RAILS_ENV set to development', () => {
process.env.RAILS_ENV = 'development'
process.env.NODE_ENV = 'development'
expect(require('../env')).toEqual({
expect(require('../../package/env')).toEqual({
railsEnv: 'development',
nodeEnv: 'development',
isProduction: false,
Expand All @@ -24,7 +24,7 @@ describe('Env', () => {
test('with undefined NODE_ENV and RAILS_ENV set to development', () => {
process.env.RAILS_ENV = 'development'
delete process.env.NODE_ENV
expect(require('../env')).toEqual({
expect(require('../../package/env')).toEqual({
railsEnv: 'development',
nodeEnv: 'production',
isProduction: true,
Expand All @@ -36,7 +36,7 @@ describe('Env', () => {
test('with undefined NODE_ENV and RAILS_ENV', () => {
delete process.env.NODE_ENV
delete process.env.RAILS_ENV
expect(require('../env')).toEqual({
expect(require('../../package/env')).toEqual({
railsEnv: 'production',
nodeEnv: 'production',
isProduction: true,
Expand All @@ -48,7 +48,7 @@ describe('Env', () => {
test('with a non-standard environment', () => {
process.env.RAILS_ENV = 'staging'
process.env.NODE_ENV = 'staging'
expect(require('../env')).toEqual({
expect(require('../../package/env')).toEqual({
railsEnv: 'staging',
nodeEnv: 'production',
isProduction: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
// environment.js expects to find config/shakapacker.yml and resolved modules from
// the root of a Rails project

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

const rootPath = process.cwd()
chdirTestApp()

const { resolve } = require('path')

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

describe('Base config', () => {
beforeEach(() => jest.resetModules() && resetEnv())
Expand All @@ -30,7 +30,7 @@ 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("../../config");
const config = require("../../../package/config");

expect(config.css_extract_ignore_order_warnings).toEqual(true)
})
Expand All @@ -47,8 +47,8 @@ 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("../../config");
const baseConfig = require('../base')
const config = require("../../../package/config");
const baseConfig = require('../../../package/environments/base')

expect(config.nested_entries).toEqual(true)

Expand All @@ -72,7 +72,7 @@ describe('Base config', () => {
})

test('should return default loader rules for each file in config/loaders', () => {
const rules = require('../../rules')
const rules = require('../../../package/rules')

const defaultRules = Object.keys(rules)
const configRules = baseConfig.module.rules
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* global test expect, describe, afterAll, beforeEach */

const { chdirTestApp, resetEnv } = require('../../utils/helpers')
const { chdirTestApp, resetEnv } = require('../../helpers')
const rootPath = process.cwd()
chdirTestApp()

Expand All @@ -14,9 +14,9 @@ describe('Development specific config', () => {

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

expect(environmentConfig.output.filename).toEqual('js/[name]-[contenthash].js')
expect(environmentConfig.output.chunkFilename).toEqual(
Expand All @@ -27,9 +27,9 @@ describe('Development specific config', () => {

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

expect(environmentConfig.output.filename).toEqual('js/[name].js')
expect(environmentConfig.output.chunkFilename).toEqual(
Expand All @@ -40,9 +40,9 @@ describe('Development specific config', () => {

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

expect(environmentConfig.output.filename).toEqual('js/[name].js')
expect(environmentConfig.output.chunkFilename).toEqual(
Expand Down
Loading

0 comments on commit 173723a

Please sign in to comment.