While technically a Sherlock Holmes story, the Valley of Fear was actually about (spoiler) an undercover Pinkerton detective infiltrating a gang in a mining town before turning them in(and then being murdered by Moriarty.... spoilers again).
I know there a fair few spies stories throughout history, but pretending to be a criminal to join a criminal organization and then betray them to the law? Was this a literary thing before Doyle?
Hi, since this is a literature question, it may be worth covering your bases by x-posting to /r/AskLiteraryStudies
I've only found this question belatedly, so I'm not sure if you're still seeking an answer to his question, but you can find a prominent earlier example in G.K. Chesterton's The Man Who Was Thursday (1908), in which a poet is hired by the police to infiltrate an anarchist group and inform on/disrupt it. It's extremely weird and very entertaining.