Path: blob/main/resolvers/webpack/test/config.js
829 views
'use strict';12const chai = require('chai');3const expect = chai.expect;4const path = require('path');56const resolve = require('../index').resolve;78const file = path.join(__dirname, 'files', 'src', 'jsx', 'dummy.js');9const extensionFile = path.join(__dirname, 'config-extensions', 'src', 'dummy.js');1011const absoluteSettings = {12config: path.join(__dirname, 'files', 'some', 'absolute.path.webpack.config.js'),13};1415describe('config', function () {16it('finds webpack.config.js in parent directories', function () {17expect(resolve('main-module', file)).to.have.property('path')18.and.equal(path.join(__dirname, 'files', 'src', 'main-module.js'));19});2021it('finds absolute webpack.config.js files', function () {22expect(resolve('foo', file, absoluteSettings)).to.have.property('path')23.and.equal(path.join(__dirname, 'files', 'some', 'absolutely', 'goofy', 'path', 'foo.js'));24});2526it('finds compile-to-js configs', function () {27const settings = {28config: path.join(__dirname, './files/webpack.config.babel.js'),29};3031expect(resolve('main-module', file, settings))32.to.have.property('path')33.and.equal(path.join(__dirname, 'files', 'src', 'main-module.js'));34});3536it('finds compile-to-js config in parent directories', function () {37expect(resolve('main-module', extensionFile))38.to.have.property('path')39.and.equal(path.join(__dirname, 'config-extensions', 'src', 'main-module.js'));40});4142it('finds the first config with a resolve section', function () {43const settings = {44config: path.join(__dirname, './files/webpack.config.multiple.js'),45};4647expect(resolve('main-module', file, settings)).to.have.property('path')48.and.equal(path.join(__dirname, 'files', 'src', 'main-module.js'));49});5051it('finds the config at option config-index', function () {52const settings = {53config: path.join(__dirname, './files/webpack.config.multiple.js'),54'config-index': 2,55};5657expect(resolve('foo', file, settings)).to.have.property('path')58.and.equal(path.join(__dirname, 'files', 'some', 'goofy', 'path', 'foo.js'));59});6061it("doesn't swallow config load errors (#435)", function () {62const settings = {63config: path.join(__dirname, './files/webpack.config.garbage.js'),64};65expect(function () { resolve('foo', file, settings); }).to.throw(Error);66});6768it('finds config object when config is an object', function () {69const settings = {70config: require(path.join(__dirname, 'files', 'some', 'absolute.path.webpack.config.js')),71};72expect(resolve('foo', file, settings)).to.have.property('path')73.and.equal(path.join(__dirname, 'files', 'some', 'absolutely', 'goofy', 'path', 'foo.js'));74});7576it('finds config object when config uses a path relative to working dir', function () {77const settings = {78config: './test/files/some/absolute.path.webpack.config.js',79};80expect(resolve('foo', file, settings)).to.have.property('path')81.and.equal(path.join(__dirname, 'files', 'some', 'absolutely', 'goofy', 'path', 'foo.js'));82});8384it('finds the first config with a resolve section when config is an array of config objects', function () {85const settings = {86config: require(path.join(__dirname, './files/webpack.config.multiple.js')),87};8889expect(resolve('main-module', file, settings)).to.have.property('path')90.and.equal(path.join(__dirname, 'files', 'src', 'main-module.js'));91});9293it('finds the config at option config-index when config is an array of config objects', function () {94const settings = {95config: require(path.join(__dirname, './files/webpack.config.multiple.js')),96'config-index': 2,97};9899expect(resolve('foo', file, settings)).to.have.property('path')100.and.equal(path.join(__dirname, 'files', 'some', 'goofy', 'path', 'foo.js'));101});102103it('finds the config at option env when config is a function', function () {104const settings = {105config: require(path.join(__dirname, './files/webpack.function.config.js')),106env: {107dummy: true,108},109};110111expect(resolve('bar', file, settings)).to.have.property('path')112.and.equal(path.join(__dirname, 'files', 'some', 'goofy', 'path', 'bar.js'));113});114115it('finds the config at option env when config is an array of functions', function () {116const settings = {117config: require(path.join(__dirname, './files/webpack.function.config.multiple.js')),118env: {119dummy: true,120},121};122123expect(resolve('bar', file, settings)).to.have.property('path')124.and.equal(path.join(__dirname, 'files', 'some', 'goofy', 'path', 'bar.js'));125});126127it('passes argv to config when it is a function', function () {128const settings = {129config: require(path.join(__dirname, './files/webpack.function.config.js')),130argv: {131mode: 'test',132},133};134135expect(resolve('baz', file, settings)).to.have.property('path')136.and.equal(path.join(__dirname, 'files', 'some', 'bar', 'bar.js'));137});138139it('passes a default empty argv object to config when it is a function', function () {140const settings = {141config: require(path.join(__dirname, './files/webpack.function.config.js')),142argv: undefined,143};144145expect(function () { resolve('baz', file, settings); }).to.not.throw(Error);146});147148it('prevents async config using', function () {149const settings = {150config: require(path.join(__dirname, './files/webpack.config.async.js')),151};152const result = resolve('foo', file, settings);153154expect(result).not.to.have.property('path');155expect(result).to.have.property('found').to.be.false;156});157});158159160