Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feature seems to try to resolve a conditional environment setting,
To preserve the project settings as much as possible and keep it centralized, I suggest follow the previous method using a Babel feature to solve this problem.

https://babeljs.io/docs/en/6.26.3/babelrc#env-option

Also recommend not include this commit to keep the tree changes clean and simple (rebase from the begining of this PR)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't find any way to do this with package.json, can you show me a example?

Copy link
Copy Markdown
Member

@rubeniskov rubeniskov Mar 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the configuration available in .babelrc are compatible inside the package.sjon babel declaration.

https://babeljs.io/docs/en/6.26.3/babelrc#use-via-packagejson

{
   "babel": {
      "env": {
         // Specific configuration for each enviroment
         "test": {
            ....
          }
      }
   }
}

const testConfig = {
presets: [
[
'@babel/preset-env',
{
targets: {
node: 'current',
},
},
],
'@babel/preset-react',
],
};

const config = {
presets: [
[
'@babel/preset-env',
{
targets: {
ie: '11',
},
useBuiltIns: 'usage',
},
],
'@babel/preset-react',
],
plugins: [
'@babel/plugin-syntax-dynamic-import',
'@babel/plugin-proposal-class-properties',
'@babel/plugin-proposal-export-namespace-from',
],
};

module.exports = (api) => {
const isTest = api.env('test');
if (isTest) return testConfig;

return config;
};
91 changes: 42 additions & 49 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"description": "React utilities for internationalization",
"main": "index.js",
"scripts": {
"test": "cross-env NODE_ENV=test nyc --reporter=html --reporter=text ava",
"test:watch": "cross-env NODE_ENV=test ava --watch",
"test": "jest --watch",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally the test script, must be a self terminated command in order to keep the expected behaviour for some CI runners (execute -> read exit code).

I suggest create the opposite way (test:watch) also npm provides a method to call a script and pass parameters to the internal command. (npm test -- --watch)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In reallity I want to do a diferent approach, I want to make npm test be watcher when the environment var CI it's not defined. Which it's something we should add when doing CI/CD.
But I will try to do this in a furure PR

"test:nowatch": "jest",
"postbuild": "./scripts/nlink lib",
"build": "babel src --out-dir lib --ignore **/*.spec.js",
"build": "babel src --out-dir lib --ignore **/*.test.js",
"prepublishOnly": "npm run build",
"prepack": "npm run build",
"lint": "eslint src",
Expand All @@ -21,7 +21,7 @@
"husky": {
"hooks": {
"pre-commit": "npm run lint",
"pre-push": "npm run lint && npm test"
"pre-push": "npm run lint && npm run test:nowatch"
}
},
"keywords": [
Expand All @@ -47,41 +47,6 @@
}
],
"license": "MIT",
"babel": {
"presets": [
[
"@babel/preset-env",
{
"targets": {
"ie": "11"
},
"useBuiltIns": "usage"
}
],
"@babel/preset-react"
],
"plugins": [
"@babel/plugin-syntax-dynamic-import",
"@babel/plugin-proposal-class-properties",
"@babel/plugin-proposal-export-namespace-from"
]
},
"ava": {
"files": [
"src/**/*.spec.js"
],
"require": [
"@babel/register",
"./test/shims"
]
},
"nyc": {
"exclude": [
"src/**/index.js",
"src/**/*.spec.js",
"test"
]
},
"dependencies": {
"hoist-non-react-statics": "^3.3.0",
"memoize-one": "^5.0.0"
Expand All @@ -96,27 +61,55 @@
"@babel/preset-react": "^7.0.0",
"@babel/register": "^7.0.0",
"@k14v/i18njs": "0.0.8",
"ava": "^1.2.1",
"babel-eslint": "^10.0.1",
"browser-env": "^3.2.5",
"cross-env": "^5.2.0",
"enzyme": "^3.8.0",
"enzyme-adapter-react-16": "^1.9.1",
"@testing-library/jest-dom": "^5.3.0",
"@testing-library/react": "^10.0.1",
"@testing-library/user-event": "^10.0.0",
"babel-eslint": "^10.1.0",
"babel-jest": "^25.2.3",
"babel-loader": "8.0.6",
"babel-plugin-named-asset-import": "^0.3.6",
"babel-preset-react-app": "^9.1.1",
"eslint": "^5.13.0",
"eslint-config-airbnb": "^17.1.0",
"eslint-plugin-import": "^2.16.0",
"eslint-plugin-jsx-a11y": "^6.1.1",
"eslint-plugin-react": "^7.12.4",
"husky": "^1.3.1",
"nyc": "^13.2.0",
"jest": "^25.2.3",
"jest-environment-jsdom-fourteen": "^1.0.1",
"jest-resolve": "^25.2.3",
"prop-types": "^15.7.2",
"react": "^16.8.4",
"react-dom": "^16.8.4",
"sinon": "^7.2.3"
"react": "^16.13.1",
"react-dom": "^16.13.1"
},
"peerDependencies": {
"prop-types": "^15.7.2",
"react": "^16.8.4",
"react-dom": "^16.8.4"
},
"jest": {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems some configurations can be simplfied

"roots": [
"<rootDir>/src"
],
"collectCoverageFrom": [
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is redudant theres no other format inside src directory

"src/**/*.{js,jsx}"
],
"setupFilesAfterEnv": [
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file has a previous review to see if is really need it

"<rootDir>/src/setupTests.js"
],
"testMatch": [
"<rootDir>/src/**/__tests__/**/*.{js,jsx}",
"<rootDir>/src/**/*.test.{js,jsx}"
],
"testEnvironment": "jest-environment-jsdom-fourteen",
"transform": {
"^.+\\.(js|jsx)$": "<rootDir>/node_modules/babel-jest"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case the project is not and react app so there's no other kind of files rather than js

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea was to have the same config in all the projects, not just the bare minimun

},
"transformIgnorePatterns": [
"[/\\\\]node_modules[/\\\\].+\\.(js|jsx)$"
],
"modulePaths": [
"<rootDir>/src"
]
}
}
53 changes: 0 additions & 53 deletions src/I18nContext.spec.js

This file was deleted.

17 changes: 17 additions & 0 deletions src/I18nContext.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Core
import React from 'react';
// Testing
import { render } from '@testing-library/react';
import { Consumer } from './testUtils';


describe('I18nConsumer', () => {

it('should throw a warning, when try to use a consumer without provider', () => {
jest.spyOn(console, 'warn');
render(<Consumer />);
expect(console.warn).toBeCalledTimes(1);
console.warn.mockRestore();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would be more precise if the test checks the content of the message

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rigth, I will make the change

});

});
94 changes: 0 additions & 94 deletions src/I18nProvider.spec.js

This file was deleted.

30 changes: 30 additions & 0 deletions src/I18nProvider.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Core
import React from 'react';
// Testing
import { render } from '@testing-library/react';
import { fetchI18n, Consumer } from './testUtils';
// Components
import I18nProvider from './I18nProvider';


describe('I18nProvider', () => {
it('render without errors', async () => {
const i18n = await fetchI18n();
render(<I18nProvider i18n={i18n}><Consumer /></I18nProvider>);
});

it('translates correctly', async () => {
const i18n = await fetchI18n();
const { getByText } = render(<I18nProvider i18n={i18n}><Consumer word="Teléfono" /></I18nProvider>);
expect(getByText('Phone')).toBeInTheDocument();
});

it('updates correctly after setLocale', async () => {
const i18n = await fetchI18n();
const { getByText, rerender } = render(<I18nProvider i18n={i18n}><Consumer word="Teléfono" /></I18nProvider>);
expect(getByText('Phone')).toBeInTheDocument();
await i18n.setLocale('de');
rerender(<I18nProvider i18n={i18n}><Consumer word="Teléfono" /></I18nProvider>);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Executing the tests, I receive this error
image

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that error is resolved in a future PR

expect(getByText('Telefon')).toBeInTheDocument();
});
});
5 changes: 5 additions & 0 deletions src/setupTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// jest-dom adds custom jest matchers for asserting on DOM nodes.
// allows you to do things like:
// expect(element).toHaveTextContent(/react/i)
// learn more: https://github.com/testing-library/jest-dom
require('@testing-library/jest-dom/extend-expect');
Loading