-
-
Notifications
You must be signed in to change notification settings - Fork 143
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
Can't seem to test with React 16, Jest, Enzyme 3.3 #52
Comments
Hi @kamranayub, this error is not caused by IdleTImer. It's because you are not exporting your app component and the test suite cant find it. Ive updated your example here. |
Also, I would probably use |
Oops, sorry, bad repro. I still get the issue locally with everything
exported but let me try and reproduce that.
…On Mon, Aug 27, 2018, 11:43 Randy Lebeau ***@***.***> wrote:
Also, I would probably use IdleTime as a standalone component and not as a
wrapper component. <IdleTImer /> vs <IdleTimer>{{some
content}}</IdleTimer>. This way your app state is separate from idle time
state.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#52 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAiaa6s_s2HCPs24Q0JCppjaz1iSrOI-ks5uVCGqgaJpZM4WOEeE>
.
|
Ah hah! I figured it out. It was due to how react-idle-timer is exported as a module and how I described it in TypeScript. This declaration file works as expected: declare module "react-idle-timer" {
import * as React from "react";
class IdleTimer extends React.Component<IdleTimerProps> {
/**
* Restore initial state and restart timer
*/
reset(): void;
/**
* Store remaining time and stop timer
*/
pause(): void;
/**
* Resumes a paused timer
*/
resume(): void;
/**
* Time remaining before idle (number of ms)
*/
getRemainingTime(): number;
/**
* How much time has elapsed (timestamp)
*/
getElapsedTime(): number;
/**
* Last time the user was active
*/
getLastActiveTime(): number;
/**
* Returns wether or not the user is idle
*/
isIdle(): boolean;
}
namespace IdleTimer {
}
interface IdleTimerProps {
ref: (ref: IdleTimer) => any;
/**
* Activity Timeout in milliseconds default: 1200000
*/
timeout?: number;
/**
* DOM events to listen to default: see [default events](https://github.com/SupremeTechnopriest/react-idle-timer#default-events)
*/
events?: string[];
/**
* Element reference to bind activity listeners to default: document
*/
element?: Node;
/**
* Start the timer on mount default: true
*/
startOnMount?: boolean;
/**
* Bind events passively default: true
*/
passive?: boolean;
/**
* Capture events default: true
*/
capture?: boolean;
/**
* Function to call when user becomes active
*/
onActive?: () => void;
/**
* Function to call when user is idle
*/
onIdle?: () => void;
}
export = IdleTimer;
} I may try to contribute a |
@kamranayub Please do! That would be great! |
OK, this was actually a little more involved. I was having the issue I originally posted above in my CRA TS project when running Jest tests. I had no problems using the component in my application at runtime, though. My
For whatever reason, this module type causes a problem when trying to use the following import syntax:
While this works at runtime (webpack can import it properly), Jest cannot handle this and throws the error above (it's basically The import has to look like this when running through tests:
This will work with the Jest tests. So to handle both working at runtime and working during tests, I introduced a These are the TS definitions I eventually ended up with: declare module "react-idle-timer" {
import * as React from "react";
class IdleTimer extends React.Component<IdleTimerProps> {
/**
* Restore initial state and restart timer
*/
reset(): void;
/**
* Store remaining time and stop timer
*/
pause(): void;
/**
* Resumes a paused timer
*/
resume(): void;
/**
* Time remaining before idle (number of ms)
*/
getRemainingTime(): number;
/**
* How much time has elapsed (timestamp)
*/
getElapsedTime(): number;
/**
* Last time the user was active
*/
getLastActiveTime(): number;
/**
* Returns wether or not the user is idle
*/
isIdle(): boolean;
}
export interface IdleTimerProps {
ref: (ref: IdleTimer) => any;
/**
* Activity Timeout in milliseconds default: 1200000
*/
timeout?: number;
/**
* DOM events to listen to default: see [default events](https://github.com/SupremeTechnopriest/react-idle-timer#default-events)
*/
events?: string[];
/**
* Element reference to bind activity listeners to default: document
*/
element?: Node;
/**
* Start the timer on mount default: true
*/
startOnMount?: boolean;
/**
* Bind events passively default: true
*/
passive?: boolean;
/**
* Capture events default: true
*/
capture?: boolean;
/**
* Function to call when user becomes active
*/
onActive?: () => void;
/**
* Function to call when user is idle
*/
onIdle?: () => void;
}
export default IdleTimer;
} The weird part is I can't repro this exactly in CodeSandbox. It apparently works in tests and at runtime with https://codesandbox.io/s/n0y232k4p0 🤷♂️ |
Sounds like it's the way default exports are handled by typescript? I'm not too familiar with ts. I like it in concept but haven't really done anything with it. I see that react has the same style of import Glad you got something working... but not sure theres anything I can do on my end to help you clean it up. If it helps, I use |
I saw that. I'd need to do more digging but I wonder if it depends on
what's consuming the entry point, at runtime it's webpack and at test time
it's ts-jest and I wonder if that CJS vs. ES entry point could be causing
issues with my configuration. I'd need to reproduce locally on my PC with a
vanilla CRA TS app to pin it down.
…On Mon, Aug 27, 2018, 15:50 Randy Lebeau ***@***.***> wrote:
Sounds like it's the way default exports are handled by typescript? I'm
not too familiar with ts. I like it in concept but haven't really done
anything with it. I see that react has the same style of import import *
as React from "react"; where usually you can just import React from
'react'?
Glad you got something working... but not sure theres anything I can do on
my end to help you clean it up. If it helps, I use Rollup to build the
component. The main export is a cjs module and the module export is an es
module. See here
<https://github.com/SupremeTechnopriest/react-idle-timer/blob/master/package.json#L5-L6>
.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#52 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAiaa2lLF5kEJEsOjP7OHYf3MuLM7SO5ks5uVFungaJpZM4WOEeE>
.
|
I had the same problem. It seems that typescript cannot pick |
Hi @kamranayub , would happen to explain to me how to implement this? sorry i am new to this but i am creating an SPFX app and i need to check a user's idle time. i have forked your codesandbox and tried to implement the example in the readme but i can't make it work. |
I've made a reproduction here; the IdleTimer works in the app but does not work when mounted during a test:
https://codesandbox.io/s/6vzxx83r0z
I get the error:
I'm specifically using the same
enzyme
andenzyme-adapter-react-16
versions listed in the package.json.The text was updated successfully, but these errors were encountered: